How to clear the existing data in react state variable (Array) and assign another array to it. I have try below thing but it doesn't works.
itemsChanged(items) {
this.setState({item : []}) //item is my state variable
this.setState({item : items})
console.log("ITEMS : ",this.state.item) //this will not print the updated value
}
Thanks in advance
this.setState({ item: items })should already override the existing array if it is a different collection of items.setStateis asynchronous so your console may be firing before the action is actually completedthis.setState({item : []}, function() {console.log("ITEMS : ",this.state.item)})