5
In the React documentation he brings an example of form, when the input
receives some property value onChange
is called with the function handleSubmit(event)
in that way onChange={this.handleChange}
because I don’t need to define the parameter in this case, since the function signature has a parameter?
class NameForm extends React.Component {
constructor(props) {
super(props);
this.state = {value: ''};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({value: event.target.value});
}
handleSubmit(event) {
alert('A name was submitted: ' + this.state.value);
event.preventDefault();
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Name:
<input type="text" value={this.state.value} onChange={this.handleChange} />
</label>
<input type="submit" value="Submit" />
</form>
);
}
how does he know exactly which parameter is right? like how he knows he has to use Event or anything?
– Davi Wesley
He doesn’t know exactly, this is not characteristic of JS or React. You have to do right what he expects, he does what he has to do and hopes that his part is ok, if it’s not going to be wrong. It’s a contract he makes with you and expects you to keep. Even in languages and technologies that guarantee this most, the guarantee goes up to a certain point, it is as you make a
for
in a whole array, if you do wrong will give compilation, or execution or logic error.– Maniero