Tuz
Tuz

Reputation: 1980

React Hook useEffect has missing dependencies: 'dispatch' and 'init' - when using useDispach in useEffect

I am trying to make some kind of initialization state in redux, so when the app is loading the app will grab some data from redux (with thunk) and will get the data.

so I need it only once and for that I put [] in the useFffect params , but I got the following error:

  Line 32:6:  React Hook useEffect has missing dependencies: 'dispatch' and 'init'. Either include them or remove the dependency array  react-hooks/exhaustive-deps

and I could not insert the useDispatch into the useEffect

  const { isReady } = useSelector<AppState, AppProps>((state: AppState) => {
    return {
      isReady: state.appStatus.isReady
    };
  });

  const dispatch = useDispatch();
  const init = initilizeAction();

  useEffect(() => {
    dispatch(init);
  }, []);

Upvotes: 0

Views: 234

Answers (1)

Deykun
Deykun

Reputation: 1271

You can move initilizeAction() to the useEffect, and add dispatch as a dependency (it should not change so it will be triggered only once).

  const dispatch = useDispatch();

  useEffect(() => {
    const init = initilizeAction();
    dispatch(init);
  }, [dispatch]);

Upvotes: 1

Related Questions