YES I know this is a common question, however I can not seem to get provided examples to work in my situation... I have an empty array in my state:
constructor(props) {
super(props);
this.state = {
selectedItems: []
}
}
And I want to simply add items to that array with this code (it's in a function):
this.setState( {
selectedItems:[...this.state.selectedItems, data]
})
data in this case being the thing added to the state array.
The problem I encounter(when I console.log this.state) is that the first occurrence of this push to the array never works. SO
for example, if I pushed to it 4 different times, only three will be in the state array and it will not register the first time I tried.
I am lost as to why. Any help appreciate. =)