This is a really strange issue I've been trying to debug today; When the component mounts I'm getting the notifications to display them like this
componentDidMount() {
this.setState(
{
notifications: this.props.notifications,
}
);
}
And then I tried to console log the notifications by adding this
componentDidMount() {
this.setState(
{
notifications: this.props.notifications,
},
() => {
console.log(this.state.notifications);
console.log(this.state.notifications.length);
}
);
}
And what I get is this!
first console.log()
[]
0: {title: "Success", text: "You have successfully created a new game!", destination: {…}, type: "GameCreated"}
length: 1
__proto__: Array(0)
second console.log()
0
Shouldn't the array length be 1 instead of 0? I do have an element in that array. Also when I try to get the first value to that array it returns undefined.
Thanks for taking the time to read this and help out!
useStateis async in nature. So if you try to access it before it sets the state, it is gonna give error