Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

196
Vistas
how to use value of useState after init it

I these state hook:

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

my medicalProblem variable will initial with response of a 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) => {
        
      });
  };

but I got this error:

Cannot read properties of undefined (reading 'features')
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

There are a couple ways to accomplish this, but the easiest may be creating another useEffect hook that depends on the medicalProblems array as seen below!

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

That extra hook will make sure your medicalProblems are populated before proceeding to initialize your features. Let me know if this works!

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda