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

201
Views
¿Cómo resolver la advertencia de matriz de dependencia en React cuando la variable es condicional?

Estoy haciendo una solicitud de recuperación en useEffect hook. Es como se ve.

 useEffect(() => { // clear prev state setUniqueValues([]); setLoading(true); // get unique values and set field values; jwtAxios .post(`${baseURL}/support/get_unique_values`, { field: fieldName, catalog_id: catalogID, page, category_values: props?.initialValues?.schema?.category, }) .then((data) => { setUniqueValues(data.data.unique_values); setLoading(false); setError(false); }) .catch((err) => { console.log(err); setError(err); }); }, [catalogID, fieldName, page]);

En la solicitud de publicación, quiero enviar un valor (category_values) si existe, si viene con accesorios. entonces compruebo con ?.

Pero si no lo pongo en la matriz de dependencias, se queja:

 warning React Hook useEffect has a missing dependency: 'props?.initialValues?.schema?.category'. Either include it or remove the dependency array react-hooks/exhaustive-deps

Si lo agrego como [catalogID, fieldName, page,props?.initialValues?.schema?.category]

Se queja como:

 Line 100:8: React Hook useEffect has a missing dependency: 'props.initialValues.schema.category'. Either include it or remove the dependency array react-hooks/exhaustive-deps Line 100:37: React Hook useEffect has a complex expression in the dependency array. Extract it to a separate variable so it can be statically checked react-hooks/exhaustive-deps

Si no pongo ? cuando no se pasan los accesorios, se colapsa porque no encuentra los accesorios.

¿Cómo lo soluciono? Gracias.

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

0

puede calcular la categoría justo antes de llamar a useEffect

 const category = props?.initialValues?.schema?.category; useEffect(() => { setUniqueValues([]); setLoading(true); jwtAxios .post(`${baseURL}/support/get_unique_values`, { field: fieldName, catalog_id: catalogID, page, category_values: category, }) ... }, [catalogID, fieldName, page, category]);
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!