Skip to content

Commit b6cbd2d

Browse files
committed
Update hooks docs with TS types and better descriptions
1 parent 9a617b3 commit b6cbd2d

File tree

1 file changed

+62
-27
lines changed

1 file changed

+62
-27
lines changed

docs/api/hooks.md

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,49 @@ From there, you may import any of the listed React Redux hooks APIs and use them
4444

4545
## `useSelector()`
4646

47-
```js
48-
const result: any = useSelector(selector: Function, equalityFn?: Function)
47+
```ts
48+
type RootState = ReturnType<typeof store.getState>
49+
type SelectorFn = <Selected>(state: RootState) => Selected
50+
type EqualityFn = (a: any, b: any) => boolean
51+
export type StabilityCheck = 'never' | 'once' | 'always'
52+
53+
interface UseSelectorOptions {
54+
equalityFn?: EqualityFn
55+
stabilityCheck?: StabilityCheck
56+
}
57+
58+
const result: Selected = useSelector(
59+
selector: SelectorFunction,
60+
options?: EqualityFn | UseSelectorOptions
61+
)
4962
```
5063

51-
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.
5265

5366
:::info
5467

5568
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.
5669

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+
5772
:::
5873

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.
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.
6077

61-
However, there are some differences between the selectors passed to `useSelector()` and a `mapState` function:
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).
6279

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).
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.
6883

6984
:::info
7085

7186
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.
7287

7388
:::
7489

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-
7790
### Equality Comparisons and Updates
7891

7992
When the function component renders, the provided selector function will be called and its result will be returned
@@ -96,10 +109,10 @@ every time will _always_ force a re-render by default. If you want to retrieve m
96109
```js
97110
import { shallowEqual, useSelector } from 'react-redux'
98111

99-
// later
112+
// Pass it as the second argument directly
100113
const selectedData = useSelector(selectorReturningObject, shallowEqual)
101114

102-
// or with object format
115+
// or pass it as the `equalityFn` field in the options argument
103116
const selectedData = useSelector(selectorReturningObject, {
104117
equalityFn: shallowEqual,
105118
})
@@ -247,15 +260,23 @@ export const App = () => {
247260

248261
### Development mode checks
249262

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+
250271
#### Selector result stability
251272

252-
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).
253274

254-
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.
255276

256277
```ts
257-
// this selector will return a new object reference whenever called
258-
// 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
259280
const { count, user } = useSelector((state) => ({
260281
count: state.count,
261282
user: state.user,
@@ -281,14 +302,20 @@ function Component() {
281302
}
282303
```
283304

284-
:::info
285-
This check is disabled for production environments.
286-
:::
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
287313

288314
## `useDispatch()`
289315

290-
```js
291-
const dispatch = useDispatch()
316+
```ts
317+
import type { Dispatch } from 'redux'
318+
const dispatch: Dispatch = useDispatch()
292319
```
293320

294321
This hook returns a reference to the `dispatch` function from the Redux store. You may use it to dispatch actions as needed.
@@ -364,8 +391,9 @@ export const Todos = () => {
364391

365392
## `useStore()`
366393

367-
```js
368-
const store = useStore()
394+
```ts
395+
import type { Store } from 'redux'
396+
const store: Store = useStore()
369397
```
370398

371399
This hook returns a reference to the same Redux store that was passed in to the `<Provider>` component.
@@ -378,12 +406,19 @@ This hook should probably not be used frequently. Prefer `useSelector()` as your
378406
import React from 'react'
379407
import { useStore } from 'react-redux'
380408

381-
export const CounterComponent = ({ value }) => {
409+
export const ExampleComponent = ({ value }) => {
382410
const store = useStore()
383411

412+
const onClick = () => {
413+
// Not _recommended_, but safe
414+
// This avoids subscribing to the state via `useSelector`
415+
// Prefer moving this logic into a thunk instead
416+
const numTodos = store.getState().todos.length
417+
}
418+
384419
// EXAMPLE ONLY! Do not do this in a real app.
385420
// The component will not automatically update if the store state changes
386-
return <div>{store.getState()}</div>
421+
return <div>{store.getState().todos.length}</div>
387422
}
388423
```
389424

0 commit comments

Comments
 (0)