I am rendering an array in a modal. Once the modal closes I need to empty the array.The following code updates the array but not clear array on click of closeModal.
constructor(props,context) {
super(props,context);
this.state = {
myArray: []
};
}
pushData(newVar) {
this.setState((state) => {
myArray: state.myArray.push(newVar)
});
}
closeModal() {
this.setState({
myArray: []
})
}
state.myArray.push(newVar)will not behave as expected either.Array.prototype.pushreturns the length of the new array, not the array with the new value in it.setState(state => ({myArray: state.myArray.concat(newVar)}))is closer to what you're looking for.