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

195
Views
cómo usar el valor de useState después de iniciarlo

Yo estos estado gancho:

 const [features, setFeatures] = useState([]) const [medicalProblem, setMedicalProblem] = useState([])

mi variable medicalProblem inicializará con la respuesta de una API:

 useEffect(() => { getMedicalProblem() }, []); const initFeatures = (index) => { let mfeatures = medicalProblem[index].features //I got Cannot read properties of undefined (reading 'features') error mfeatures.sort(function (a, b) { return a.order - b.order; }); setFeatures(mfeatures) } const getMedicalProblem = () => { api .get("dental/generic/slideshow/medical-problem/") .then((res: any) => { const { count, results } = res setMedicalProblem(results) initFeatures(0) }) .catch((err) => { }); };

pero tengo este error:

 Cannot read properties of undefined (reading 'features')
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Hay un par de formas de lograr esto, pero la más fácil puede ser crear otro enlace useEffect que dependa de la matriz medicalProblems como se ve a continuación.

 useEffect(() => { if (medicalProblems) { initFeatures(0) } }, [medicalProblems])

Ese enlace adicional se asegurará de que sus problemas médicos estén completos antes de proceder a inicializar sus funciones. ¡Hazme saber si esto funciona!

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!