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

87
Views
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
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!