1

The lint rules force me to include 'dispatch' also as a dependency for useEffect().

(If I don't add dispatch as dependency, it throws warning that "React Hook useEffect has a missing dependency: 'dispatch'. Either include it or remove the dependency array.")

Is it okay to list dispatch or any other function as dependency? Is there a better way to use without disabling the lint rule?

useEffect(() => {
    if (debouncedSearchText) {
      dispatch(getUsers(pageIndex, rows, debouncedSearchText));
    }
},[debouncedSearchText, pageIndex, dispatch]);

1 Answer 1

1

It's alright to add functions as dependencies, but keep in mind that the functions which are in the hierarchy of your component should be wrapped in useCallback hook, as on re-render it's not going to change the reference unless dependencies given in the useCallback are changed. Library writers already exposes memoized functions so no need to wrap those functions in useCallback.

Sign up to request clarification or add additional context in comments.

1 Comment

As a note, dispatch is already stable, so it's fine to include in the dependency array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.