I have an array in my state in my React Native project, like this:
constructor(props) {
super(props)
this.state = {
a: [1, 2, 3]
}
}
I click a button to update the array, like this:
<TouchableOpacity
onPress={() => {this.setState({a: [5, 6]})}
/>
I want to verify the state change in my componentDidUpdate()
method, like this:
componentDidUpdate(prevProps, prevState) {
console.log("prevState: ", prevState)
console.log("state: ", this.state)
}
but this always gives
prevState: (5) [-1, -1, -1, -1, -1]
state: (5) [-1, -1, -1, -1, -1]
I confirmed using devTools that the array is indeed updating to [5, 6], but the componentDidUpdate()
method is still just returning the old value. Any idea why?
Thanks!