Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
Not able to set state in react properly

I have this function which is setting the user

  const [user, setUser] = useState(null)

  const getUser = async () => {
    setIsAuth(false)
    setLoading(true)
    try {
      let temp = await axiosURL.get('/api/users/me')
      temp = temp.data
      if (temp) {
        setUser(temp)
        setIsAuth(true)
      }
    } catch (error) {
      console.log(error)
    }
    setLoading(false)
  }

After setting the user I am fecthing the data acording to the user

   const getAllScopeData = async () => {
    setLoading(true)
    await getUser()
    console.log(user)
    await getScopeOneData()
    await getScopeTwoData()
    await getScopeThreeData()
    setLoading(false)
  }

But when I am running getAllScopeData() The user details is fetching correctly and logging in the console but user from the state is not changing from null . So I am able to fetch further data .

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

the problem I see it's here

const getAllScopeData = async () => {
    setLoading(true)
    await getUser()
    console.log(user)
    await getScopeOneData()
    await getScopeTwoData()
    await getScopeThreeData()
    setLoading(false)
  }

even if you wait for the function getUser your state is not update yet

You should use useEffect like this

useEffect(async() => {
if(!user) return;
   setLoading(true)
    await getScopeOneData()
    await getScopeTwoData()
    await getScopeThreeData()
    setLoading(false)
}, [user])


about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!