reactjs / react.dev

The React documentation website
https://react.dev/
Creative Commons Attribution 4.0 International
11.04k stars 7.53k forks source link

Missing documentation of `dispatch` function's stability #6178

Open forman opened 1 year ago

forman commented 1 year ago

First, I like the new React docs a lot! Unfortunately, I can no longer find the following information about useReducer that was once available in the docs:

The useReducer hook returns an array with two elements: the current state and a dispatch function. The dispatch function is used to update the state by calling it with an action object. The dispatch function returned by useReducer has a lifecycle similar to that of the component it is used in. It is created when the component mounts and destroyed when the component unmounts. The dispatch function is guaranteed to be stable across re-renders.

Is this no longer the case?

On page Lifecycle of Reactive Effects there is a note box saying "In some cases, React knows that a value never changes..." and mentioning useState and useRef. It would be ideal if the documentation would explicitly enumerate all the stable return values from hooks, i.e., the ones that share the lifecycle of their component.

Afrin127329 commented 1 year ago

@forman probably this is the page you are looking for which shows the similar texts that you have highlighted: useReducer You can check with that.

rickhanlonii commented 1 year ago

Yeah, may be worth mention in the caveats of the dispatch function docs @Afrin127329 mentioned here: https://react.dev/reference/react/useReducer#dispatch. Want to submit a PR?