Tell us what’s happening:
What’s the difference between:
handleSubmit(event) {
event.preventDefault()
this.setState({
submit: this.state.input
});
}
and
handleSubmit(event) {
event.preventDefault()
this.setState(state => {
submit: state.input
});
}
Why the second one does not work?
Thanks in advance
Your code so far
class MyForm extends React.Component {
constructor(props) {
super(props);
this.state = {
input: '',
submit: ''
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({
input: event.target.value
});
}
handleSubmit(event) {
//event.preventDefault()
this.setState({
submit: this.state.input
});
}
render() {
return (
<div>
<form onSubmit={this.handleSubmit}>
{ /* change code below this line */ }
<input value={this.state.input} onChange={this.handleChange}>
</input>
{ /* change code above this line */ }
<button onClick={this.handleSubmit} type='submit'>Submit!</button>
</form>
{ /* change code below this line */ }
<h1>{this.state.submit}</h1>
{ /* change code above this line */ }
</div>
);
}
};
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36
.
Challenge: Create a Controlled Form
Link to the challenge:
https://www.freecodecamp.org/learn/front-end-libraries/react/create-a-controlled-form