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

163
Views
Using useState doesnt automatically set data?

I'm new to react so maybe I'm missing something but I've encountered something weird.

I have this useState

const [postsurl, setPostsurl] = useState([])

and then I fill it with data from a promise

setPostsurl(result.data)

When I console.log the result.data after this, it prints out the data just fine. However, when I console.log(postsurl) after setting it, it's still an empty array. I'm assuming this has to do with the way the language works. Is there any way to resolve this?

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

0

When you try to console.log() your states right after your state setter is called, your state probably would not be updated by then because React sets the state asynchronously. useEffect can help you with that, you can console.log() your state every time it changes. Something like this:

useEffect(() => {
  console.log(postsurl)
}, [postsurl])
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!