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
Allows you to extract data from the Redux store state, using a selector function.
64
+
Allows you to extract data from the Redux store state for use in this component, using a selector function.
52
65
53
66
:::info
54
67
55
68
The selector function should be [pure](https://en.wikipedia.org/wiki/Pure_function) since it is potentially executed multiple times and at arbitrary points in time.
56
69
70
+
See [Using Redux: Deriving Data with Selectors](https://redux.js.org/usage/deriving-data-selectors) in the Redux docs for more details on writing and using selector functions.
71
+
57
72
:::
58
73
59
-
The selector is approximately equivalent to the [`mapStateToProps` argument to `connect`](../using-react-redux/connect-extracting-data-with-mapStateToProps.md) conceptually. The selector will be called with the entire Redux store state as its only argument. The selector will be run whenever the function component renders (unless its reference hasn't changed since a previous render of the component so that a cached result can be returned by the hook without re-running the selector). `useSelector()`will also subscribe to the Redux store, and run your selector whenever an action is dispatched.
74
+
The selector will be called with the entire Redux store state as its only argument. The selector may return any value as a result, including directly returning a value that was nested inside `state`, or deriving new values. The return value of the selector will be used as the return value of the `useSelector()`hook.
60
75
61
-
However, there are some differences between the selectors passed to `useSelector()` and a `mapState` function:
76
+
The selector will be run whenever the function component renders (unless its reference hasn't changed since a previous render of the component so that a cached result can be returned by the hook without re-running the selector). `useSelector()`will also subscribe to the Redux store, and run your selector whenever an action is dispatched.
62
77
63
-
- The selector may return any value as a result, not just an object. The return value of the selector will be used as the return value of the `useSelector()`hook.
64
-
- When an action is dispatched, `useSelector()` will do a reference comparison of the previous selector result value and the current result value. If they are different, the component will be forced to re-render. If they are the same, the component will not re-render.
65
-
-The selector function does _not_ receive an `ownProps` argument. However, props can be used through closure (see the examples below) or by using a curried selector.
66
-
- Extra care must be taken when using memoizing selectors (see examples below for more details).
67
-
-`useSelector()`uses strict `===` reference equality checks by default, not shallow equality (see the following section for more details).
78
+
When an action is dispatched, `useSelector()` will do a reference comparison of the previous selector result value and the current result value. If they are different, the component will be forced to re-render. If they are the same, the component will not re-render. `useSelector()`uses strict `===` reference equality checks by default, not shallow equality (see the following section for more details).
79
+
80
+
The selector is approximately equivalent to the [`mapStateToProps` argument to `connect`](../using-react-redux/connect-extracting-data-with-mapStateToProps.md) conceptually.
81
+
82
+
You may call `useSelector()`multiple times within a single function component. Each call to `useSelector()` creates an individual subscription to the Redux store. Because of the React update batching behavior used in React Redux v7, a dispatched action that causes multiple `useSelector()`s in the same component to return new values _should_ only result in a single re-render.
68
83
69
84
:::info
70
85
71
86
There are potential edge cases with using props in selectors that may cause issues. See the [Usage Warnings](#usage-warnings) section of this page for further details.
72
87
73
88
:::
74
89
75
-
You may call `useSelector()` multiple times within a single function component. Each call to `useSelector()` creates an individual subscription to the Redux store. Because of the React update batching behavior used in React Redux v7, a dispatched action that causes multiple `useSelector()`s in the same component to return new values _should_ only result in a single re-render.
76
-
77
90
### Equality Comparisons and Updates
78
91
79
92
When the function component renders, the provided selector function will be called and its result will be returned
@@ -96,8 +109,13 @@ every time will _always_ force a re-render by default. If you want to retrieve m
- Use a custom equality function as the `equalityFn` argument to `useSelector()`, like:
@@ -240,10 +258,64 @@ export const App = () => {
240
258
}
241
259
```
242
260
261
+
### Development mode checks
262
+
263
+
`useSelector` runs some extra checks in development mode to watch for unexpected behavior. These checks do not run in production builds.
264
+
265
+
:::info
266
+
267
+
These checks were first added in v8.1.0
268
+
269
+
:::
270
+
271
+
#### Selector result stability
272
+
273
+
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
+
This is important, as a selector returning that returns a different result reference with the same parameter will cause unnecessary rerenders.
276
+
277
+
```ts
278
+
// this selector will return a new object reference whenever called,
279
+
// which causes the component to rerender after *every* action is dispatched
280
+
const { count, user } =useSelector((state) => ({
281
+
count: state.count,
282
+
user: state.user,
283
+
}))
284
+
```
285
+
286
+
If a selector result is suitably stable (or the selector is memoized), it will not return a different result and no warning will be logged.
287
+
288
+
By default, this will only happen when the selector is first called. You can configure the check in the Provider or at each `useSelector` call.
const user =useSelector(selectUser, { stabilityCheck: 'once' })
301
+
// ...
302
+
}
303
+
```
304
+
305
+
### Comparisons with `connect`
306
+
307
+
There are some differences between the selectors passed to `useSelector()` and a `mapState` function:
308
+
309
+
- The selector may return any value as a result, not just an object.
310
+
- The selector normally _should_ return just a single value, and not an object. If you do return an object or an array, be sure to use a memoized selector to avoid unnecessary re-renders.
311
+
- The selector function does _not_ receive an `ownProps` argument. However, props can be used through closure (see the examples above) or by using a curried selector.
312
+
- You can use the `equalityFn` option to customize the comparison behavior
313
+
243
314
## `useDispatch()`
244
315
245
-
```js
246
-
constdispatch=useDispatch()
316
+
```ts
317
+
importtype { Dispatch } from'redux'
318
+
const dispatch:Dispatch=useDispatch()
247
319
```
248
320
249
321
This hook returns a reference to the `dispatch` function from the Redux store. You may use it to dispatch actions as needed.
@@ -319,8 +391,9 @@ export const Todos = () => {
319
391
320
392
## `useStore()`
321
393
322
-
```js
323
-
conststore=useStore()
394
+
```ts
395
+
importtype { Store } from'redux'
396
+
const store:Store=useStore()
324
397
```
325
398
326
399
This hook returns a reference to the same Redux store that was passed in to the `<Provider>` component.
@@ -333,12 +406,19 @@ This hook should probably not be used frequently. Prefer `useSelector()` as your
333
406
importReactfrom'react'
334
407
import { useStore } from'react-redux'
335
408
336
-
exportconstCounterComponent= ({ value }) => {
409
+
exportconstExampleComponent= ({ value }) => {
337
410
conststore=useStore()
338
411
412
+
constonClick= () => {
413
+
// Not _recommended_, but safe
414
+
// This avoids subscribing to the state via `useSelector`
415
+
// Prefer moving this logic into a thunk instead
416
+
constnumTodos=store.getState().todos.length
417
+
}
418
+
339
419
// EXAMPLE ONLY! Do not do this in a real app.
340
420
// The component will not automatically update if the store state changes
' returned a different result when called with the same parameters. This can lead to unnecessary rerenders.'+
102
+
'\nSelectors that return a new reference (such as an object or an array) should be memoized: https://redux.js.org/usage/deriving-data-selectors#optimizing-selectors-with-memoization',
0 commit comments