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

95
Vistas
How to fulfill React useEffect missing dependency requirements?

I can't figure out, how to fulfill the useEffect missing dependency requirements in the Dashboard component, where I only want to fetch Jobs once on render. Adding fetchJobs as a dependency or removing the dependency array causes infinite re-renders.

Dashboard.js:

...
import { useGlobalContext } from "../context/appContext";

const Dashboard = () => {
  const { user, logout, fetchJobs } = useGlobalContext();

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

  return (
    ...
  );
};

export default Dashboard;

appContext.js:

...
const initialState = {
  user: null,
  jobs: []
};

const AppContext = React.createContext();

export const AppProvider = ({ children }) => {
  const [state, dispatch] = useReducer(reducer, initialState);

  ...

  const fetchJobs = async () => {
    try {
      const { data } = await API.get(`/jobs`);
      dispatch({ type: FETCH_JOBS_SUCCESS, payload: data.jobs });
    } catch (err) {
      console.log(err);
    }
  };

  ...

  return (
    <AppContext.Provider
      value={{
        ...state,
        login,
        logout,
        fetchJobs
      }}
    >
      {children}
    </AppContext.Provider>
  );
};

export const useGlobalContext = () => {
  return useContext(AppContext);
};

I tried to implement it with useCallback, but that didn't work either. Is this issue possibly related to using useContext?

I know that you can ignore the linting error by placing a // eslint-disable-next-line right above the dependency array, but that doesn't seem to be the right way to me.

Thanks for your help, I appreciate it.

Codesandbox: https://codesandbox.io/s/gracious-hamilton-ekftz3

about 4 years ago · Juan Pablo Isaza
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