I cannot wrap my head around the issue below.
The issue relates to the asynchronous setState dimension. Usually I use the callback, but doesn\'t seem appropriate here.
You can't do what you are trying here with setState
because it is asynchronous and will conflict with the different states available on each iteration in that for ()
loop.
What you can do is extract the state first, manipulate it as needed, then run setState
(at least in this one below)
.then(data => {
// Pull out what you want to mess with here first
const users = [ ...this.state.users ];
const dataValObj = data.val();
// Then use spread operator (or Object.assign/Array.concat)
this.setState({
users: [
...users,
...Object.keys(dataValObj)
],
...dataValObj
});
})
And it seems you followed a similar pattern throughout the code. Try and apply what I've done here to the other areas that are using loops with setState
inside them.