Skip to content Skip to sidebar Skip to footer

Javascript/mongodb: Usestate Problem Null Values On Initial Registration

I'm getting a problem with useState(). The problem is whenever the page refresh userId has no value because I set it but inside my code I acquire it's value. const [userId, setUser

Solution 1:

There are two things,

  1. Refreshing page actually SHOULD remove all the data react is holding. If u want to persist data in-between refreshes then use sessionStorage

  2. Setting a value via useState hook shows the value AFTER a render. So, you cannot do setUserId and in the next promise agentId: userId and expect to get the updated value. You will get the last value.

So instead of agentId: userId do agentId: userData that should be a quick fix.

Post a Comment for "Javascript/mongodb: Usestate Problem Null Values On Initial Registration"