Answer the question
In order to leave comments, you need to log in
How to generalize a method?
There are many fields that, when changed, write to the state - and each field has its own handler.
How to make one method take a parameter and understand where to write to the state?
handleChangeKpp = (kpp: string) => {
this.setState({kpp});
};
Answer the question
In order to leave comments, you need to log in
handleChange = e => {
this.setState({ [e.target.name]: e.target.value })
}
....
<input type="text" name="text1" onChange={this.handleChange} />
<input type="text" name="text2" onChange={this.handleChange} />
<input type="text" name="text3" onChange={this.handleChange} />
Here is the method that needs to be universalized, for example, it takes a parameter "name" and does setSatate({name}),
takes "position" and does setSatate({name})
setParam = (name: string, value: any) => {
this.setState({ ...this.state, [name]: value });
}
...
setParam("position", ...);
...this.state
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question