Skip to content

Commit e0da9c1

Browse files
EskiMojo14markerikson
authored andcommitted
Add docs
1 parent 80b8a51 commit e0da9c1

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

docs/api/Provider.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ interface ProviderProps<A extends Action = AnyAction, S = any> {
4343
*/
4444
context?: Context<ReactReduxContextValue<S, A>>
4545

46+
/** Global configuration for the `useSelector` stability check */
47+
stabilityCheck?: StabilityCheck
48+
4649
/** The top-level React elements in your component tree, such as `<App />` **/
4750
children: ReactNode
4851
}

docs/api/hooks.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ import { shallowEqual, useSelector } from 'react-redux'
9898

9999
// later
100100
const selectedData = useSelector(selectorReturningObject, shallowEqual)
101+
102+
// or with object format
103+
const selectedData = useSelector(selectorReturningObject, {
104+
equalityFn: shallowEqual,
105+
})
101106
```
102107

103108
- Use a custom equality function as the `equalityFn` argument to `useSelector()`, like:
@@ -240,6 +245,46 @@ export const App = () => {
240245
}
241246
```
242247

248+
### Development mode checks
249+
250+
#### Selector result stability
251+
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).
253+
254+
This is important, as a selector returning a materially different result with the same parameter will cause unnecessary rerenders.
255+
256+
```ts
257+
// this selector will return a new object reference whenever called
258+
// meaning the component will rerender whenever *any* action is dispatched
259+
const { count, user } = useSelector((state) => ({
260+
count: state.count,
261+
user: state.user,
262+
}))
263+
```
264+
265+
If a selector result is suitably stable, or memoised, it will not return a different result and thus not cause a warning to be logged.
266+
267+
By default, this will only happen when the selector is first called. You can configure the check via context, or per `useSelector` call - either to run the check always, or never.
268+
269+
```tsx title="Global setting via context"
270+
<Provider store={store} stabilityCheck="always">
271+
{children}
272+
</Provider>
273+
```
274+
275+
```tsx title="Individual hook setting"
276+
function Component() {
277+
const count = useSelector(selectCount, { stabilityCheck: 'never' })
278+
// run once (default)
279+
const user = useSelector(selectUser, { stabilityCheck: 'once' })
280+
// ...
281+
}
282+
```
283+
284+
:::info
285+
This check is disabled for production environments.
286+
:::
287+
243288
## `useDispatch()`
244289

245290
```js

src/components/Provider.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ export interface ProviderProps<A extends Action = AnyAction, S = unknown> {
2323
*/
2424
context?: Context<ReactReduxContextValue<S, A>>
2525

26+
/** Global configuration for the `useSelector` stability check */
2627
stabilityCheck?: StabilityCheck
28+
2729
children: ReactNode
2830
}
2931

0 commit comments

Comments
 (0)