React JS maintain array inside state
Issue is you are updating the state
value in a wrong way, Update the state value like this:
this.setState({
terms: this.state.terms,
myArray : this.state.myArray
});
As per DOC:
Never mutate this.state directly, as calling setState() afterwards may replace the mutation you made. Treat this.state as if it were immutable.
Update the state array
like this, first create a copy of that by using slice()
, then do the change and use setState
to update:
let arr = this.state.myarr.slice();
arr.push('data');
this.setState({arr});