hello,
Créer une entrée contrôlée
in this ligne, is it “value” really necessary with “input” in this case ?:
<input value={this.state.input} onChange={this.handleChange}/>
because when I made the exercice I didn’t put th value property and the code worked.
because for me but maybe I’m wrong, onChange() check the input, if there is a changing it send the “event” to handleChange() which send the new input’s value to the input of “state”.
So I wonder why value property is necessary here?
As I said, the code works without it.
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);
// 3
}
handleChange(event) { // 1
this.setState({
input: event.target.value
});
}
handleSubmit(event) { // 1
// Change code below this line
event.preventDefault(); // voir la note de l'exo
this.setState({
submit: this.state.input
});
// Change code above this line
}
render() {
return (
<div>
<form onSubmit={this.handleSubmit}> // 2
{/* Change code below this line */}
<input value={this.state.input} onChange={this.handleChange}/> // 2
{/* Change code above this line */}
<button type='submit'>Submit!</button>
</form>
{/* Change code below this line */}
<h1>{this.state.submit}</h1> //4
{/* Change code above this line */}
</div>
);
}
}