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
@@ -58,24 +71,22 @@ See [Using Redux: Deriving Data with Selectors](https://redux.js.org/usage/deriv
58
71
59
72
:::
60
73
61
-
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.
75
+
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.
77
+
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).
62
79
63
-
However, there are some differences between the selectors passed to `useSelector()` and a `mapState` function:
80
+
The selector is approximately equivalent to the [`mapStateToProps` argument to `connect`](../using-react-redux/connect-extracting-data-with-mapStateToProps.md) conceptually.
64
81
65
-
- 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.
66
-
- 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.
67
-
- 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.
68
-
- Extra care must be taken when using memoizing selectors (see examples below for more details).
69
-
-`useSelector()` uses strict `===` reference equality checks by default, not shallow equality (see the following section for more details).
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.
70
83
71
84
:::info
72
85
73
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.
74
87
75
88
:::
76
89
77
-
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.
78
-
79
90
### Equality Comparisons and Updates
80
91
81
92
When the function component renders, the provided selector function will be called and its result will be returned
@@ -98,10 +109,10 @@ every time will _always_ force a re-render by default. If you want to retrieve m
`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
+
252
271
#### Selector result stability
253
272
254
-
In development, an extra check is conducted on the passed selector. It runs the selector an extra time with the same parameter, and warns in console if it returns a different result (based on the `equalityFn` provided).
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).
255
274
256
-
This is important, as a selector returning a materially different result with the same parameter will cause unnecessary rerenders.
275
+
This is important, as a selector returning that returns a different result reference with the same parameter will cause unnecessary rerenders.
257
276
258
277
```ts
259
-
// this selector will return a new object reference whenever called
260
-
//meaning the component will rerender whenever *any* action is dispatched
278
+
// this selector will return a new object reference whenever called,
279
+
//which causes the component to rerender after *every* action is dispatched
261
280
const { count, user } =useSelector((state) => ({
262
281
count: state.count,
263
282
user: state.user,
@@ -283,14 +302,20 @@ function Component() {
283
302
}
284
303
```
285
304
286
-
:::info
287
-
This check is disabled for production environments.
288
-
:::
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
289
313
290
314
## `useDispatch()`
291
315
292
-
```js
293
-
constdispatch=useDispatch()
316
+
```ts
317
+
importtype { Dispatch } from'redux'
318
+
const dispatch:Dispatch=useDispatch()
294
319
```
295
320
296
321
This hook returns a reference to the `dispatch` function from the Redux store. You may use it to dispatch actions as needed.
@@ -366,8 +391,9 @@ export const Todos = () => {
366
391
367
392
## `useStore()`
368
393
369
-
```js
370
-
conststore=useStore()
394
+
```ts
395
+
importtype { Store } from'redux'
396
+
const store:Store=useStore()
371
397
```
372
398
373
399
This hook returns a reference to the same Redux store that was passed in to the `<Provider>` component.
@@ -380,12 +406,19 @@ This hook should probably not be used frequently. Prefer `useSelector()` as your
380
406
importReactfrom'react'
381
407
import { useStore } from'react-redux'
382
408
383
-
exportconstCounterComponent= ({ value }) => {
409
+
exportconstExampleComponent= ({ value }) => {
384
410
conststore=useStore()
385
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
+
386
419
// EXAMPLE ONLY! Do not do this in a real app.
387
420
// The component will not automatically update if the store state changes
0 commit comments