What is the correct way to update (concatenate) the input value with the previous value of the item of the object?
Consider the below example:
local states
this.state = { model: 'ggg'}
In handler
handleChange = (event) => {
this.setState({...this.state,
[event.target.name]: event.target.value});
};
render() {
const {values} = this.state;
return (
<TextField
id="model"
label="Model"
name="model"
value={this.state.model}
onChange={evt => this.handleChange(evt)}/>
)
};
this is updating the model while I input the characters and update the model as "ggga" "gggaaa".
But I want to update my object in state now.
local states
this.state = {values: { model: 'ggg'}}
and I tried,
handleChange = (event) => {
this.setState(prevState => ({
values: {
...prevState.values,
[event.target.name]: event.target.value
},
}))}
<TextField
id="model"
label="Model"
name="model"
value={this.state.values.model}
onChange={evt => this.handleChange(evt)}/>
Output : "ggga", "gggq"
this is not giving me the expected output. Can someone please suggest a solution.
Thank you