You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api/hooks.md
+38-5
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ description: 'API > Hooks: the `useSelector` and `useDispatch` hooks`'
10
10
11
11
# Hooks
12
12
13
-
React's new ["hooks" APIs](https://reactjs.org/docs/hooks-intro.html) give function components the ability to use local component state, execute side effects, and more. React also lets us write [custom hooks](https://reactjs.org/docs/hooks-custom.html), which let us extract reusable hooks to add our own behavior on top of React's built-in hooks.
13
+
React's ["hooks" APIs](https://react.dev/reference/react#) give function components the ability to use local component state, execute side effects, and more. React also lets us write [custom hooks](https://react.dev/learn/reusing-logic-with-custom-hooks#extracting-your-own-custom-hook-from-a-component), which let us extract reusable hooks to add our own behavior on top of React's built-in hooks.
14
14
15
15
React Redux includes its own custom hook APIs, which allow your React components to subscribe to the Redux store and dispatch actions.
16
16
@@ -48,11 +48,12 @@ From there, you may import any of the listed React Redux hooks APIs and use them
@@ -272,7 +273,7 @@ These checks were first added in v8.1.0
272
273
273
274
In development, the provided selector function is run an extra time with the same parameter during the first call to `useSelector`, and warns in the console if the selector returns a different result (based on the `equalityFn` provided).
274
275
275
-
This is important, as a selector returning that returns a different result reference with the same parameter will cause unnecessary rerenders.
276
+
This is important, as **a selector that returns a different result reference when called again with the same inputs will cause unnecessary rerenders**.
276
277
277
278
```ts
278
279
// this selector will return a new object reference whenever called,
@@ -302,6 +303,38 @@ function Component() {
302
303
}
303
304
```
304
305
306
+
#### No-op selector check
307
+
308
+
In development, a check is conducted on the result returned by the selector. It warns in the console if the result is the same as the parameter passed in, i.e. the root state.
309
+
310
+
**A `useSelector` call returning the entire root state is almost always a mistake**, as it means the component will rerender whenever _anything_ in state changes. Selectors should be as granular as possible, like `state => state.some.nested.field`.
311
+
312
+
```ts no-transpile
313
+
// BAD: this selector returns the entire state, meaning that the component will rerender unnecessarily
314
+
const { count, user } =useSelector((state) =>state)
315
+
316
+
// GOOD: instead, select only the state you need, calling useSelector as many times as needed
When passing a callback using `dispatch` to a child component, you may sometimes want to memoize it with [`useCallback`](https://reactjs.org/docs/hooks-reference.html#usecallback). _If_ the child component is trying to optimize render behavior using `React.memo()` or similar, this avoids unnecessary rendering of child components due to the changed callback reference.
376
+
When passing a callback using `dispatch` to a child component, you may sometimes want to memoize it with [`useCallback`](https://react.dev/reference/react/useCallback). _If_ the child component is trying to optimize render behavior using `React.memo()` or similar, this avoids unnecessary rendering of child components due to the changed callback reference.
[React Redux](https://github.com/reduxjs/react-redux) is the official [React](https://reactjs.org/) UI bindings layer for [Redux](https://redux.js.org/). It lets your React components read data from a Redux store, and dispatch actions to the store to update state.
16
+
[React Redux](https://github.com/reduxjs/react-redux) is the official [React](https://react.dev/) UI bindings layer for [Redux](https://redux.js.org/). It lets your React components read data from a Redux store, and dispatch actions to the store to update state.
0 commit comments