reactjs / reactjs/react.dev

Does `dispatch` function identity change when passed in context? [Re: eslint-plugin-react-hooks]

Open
#1,889 13 comments 12 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
11.8k
Forks
7.9k
Avg merge
1d 11h
Merged PRs (30d)
11

Description

Issue:
  • The docs for useReducer suggests omitting dispatch from the dependencies list of useEffect and useReducer.

    Note
    React guarantees that dispatch function identity is stable and won't change on re-renders. This is why it's safe to omit from the useEffect or useCallback dependency list.

  • The docs also recommend passing dispatch function from useReducer via context to avoid passing callbacks down a large component tree:

  • eslint-plugin-react-hooks warns if dispatch (that's been passed from context) is not added to useEffect dependencies list.

    This conflicts with the first point above (docs suggesting omitting dispatch from the dependencies list)

    React Hook useCallback has a missing dependency: 'dispatch'. Either include it or remove the dependency array.
    

Is this:

  • Expected behavior after passing dispatch through context - so the docs should have a small amendment for this scenario?
  • Or a bug in eslint-plugin-react-hooks?

Thanks for the help!!

Working Sample:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read the linked useReducer and Hooks FAQ documentation first, then reproduce the warning in the CodeSandbox and compare it with eslint-plugin-react-hooks behavior. Done means determining whether the docs need clarification or the plugin behavior needs a separate fix, with the relevant conclusion recorded.

Written by the indexing model from the issue text.

Assessment

Tech stack
eslint, javascript, react
Domain
documentation, tooling
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.