Lets say I have a React component that has a \"state\" with 10 fields:
this.state = {
field1: 1,
field2: 2,
... other fields
something: \'a\'
};
NEVER mutate this.state directly, as calling setState() afterwards may replace the mutation you made. Treat this.state as if it were immutable.
use Object.assign
for clone object
const newState = Object.assign({}, this.state, {
something: 'b'
})
this.setState(newState)
Or you can merge current state:
this.setState({
something: 'a',
something2: 'b',
})