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

101
Views
How to wait for setState prior to executing next steps that rely on setState values

I have the following useEffect hook within my react app with useState:

  const [jobTypes, setJobTypes] = useState([]);

  const getJobTypes = async () => {
    try {
      const response = await fetch('/job-types');
      const jsonData = await response.json();

      setJobTypes(jsonData);

      const jobTypeName = jobTypes.find(jobType => jobType.ret === jobDetails.jobType)
      console.log("jobTypeName: ", jobTypeName)

    } catch (err) {
      console.error(err.message);
    }
  }


  useEffect(() => {
    getJobTypes();
  }, []);

The issue I am facing is that I need to wait for setJobTypes(jsonData) to complete prior to performing my find statement, i.e:

const jobTypeName = jobTypes.find(jobType => jobType.ret === jobDetails.jobType)

as I am getting back an undefined for jobTypeName

Can anyone assist with the best approach to achieve the above as I only want to execute my array.find when jobTypes is available

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

0

I think the best way is using jsonData instead of jobTypes

const jobTypeName = jsonData.find(jobType => jobType.ret === jobDetails.jobType)
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!