1

I can see my array in state, but I don't know why elements of array doesn't display on the app interface.

const [members, setMembers] = useState([])

useEffect( () => {
    getMembers();
}, [props.event])



const getMembers = () => {
    let new_members = [];
    console.log(props.event)
    props.event &&  props.event.uczestnicy.map(member => {
        member.get().then(doc => {
            let new_member;
            new_member = {
                ...doc.data(),
                id: doc.id
            }
            new_members.push(new_member)
        })
        setMembers(new_members)
    })
    console.log(new_members)
    console.log(members)    
}

[...]

{members && members.map(member => {
    console.log('mem',member)
    return(
        <div key={member.id}>
            {member.nick}
        </div>
    )
})}

enter image description here

So I can see this array in Components using React Developer Tools, but even console.log doesn't see it in the moment of performing.

And console.log(new_members) and console.log(members) result : enter image description here

7
  • You need to wait for the promis to resolve before trying to use it. Commented May 28, 2020 at 21:45
  • Ok, but how can I do it ? and why then console display new_members but members doesn't ? Commented May 28, 2020 at 21:47
  • 1
    The first log displays the values because they are set. But setMembers requires another render before the members are actually set and the log statement is ofc executed within the same render so it is still unset in that moment. Commented May 28, 2020 at 21:50
  • Does this answer your question? React setState not updating state I know you're using useState hook, but the concept of react lifecycle is identical. This question is asked almost daily, please search to see if your issue has already been asked and has an accepted solution. Commented May 28, 2020 at 21:51
  • Also, you've no asynchronous code so each iteration of array::map updates state with the same initial empty newMembers array. Commented May 28, 2020 at 21:57

1 Answer 1

1

Your member values are fetch asynchronously, so its ideal if you set state only after all the values are resolved. For this you can use a Promise.all

const getMembers = async () => {
    let new_members = [];
    console.log(props.event)
    if(props.event) {
      const val  = await Promise.all(props.event.uczestnicy.map(member => {
        return member.get().then(doc => {
            let new_member;
            new_member = {
                ...doc.data(),
                id: doc.id
            }
            return new_member
        })
     });
     setMembers(values);
     console.log(values);
   } 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.