useSelector()
const result: Selected = useSelector(
selector: (state: RootState) => Selected,
options?: EqualityFn | UseSelectorOptions
)
added in v7.1.0
Allows you to extract data from the Redux store state for use in this component, using a selector function.
import React from 'react'
import { useSelector } from 'react-redux'
export const CounterComponent = () => {
const counter = useSelector((state) => state.counter)
return <div>{counter}</div>
}
The selector function should be pure since it is potentially executed multiple times and at arbitrary points in time.
See Using Redux: Deriving Data with Selectors in the Redux docs for more details on writing and using selector functions.
API Reference
Signature
type RootState = ReturnType<typeof store.getState>
type SelectorFn = <Selected>(state: RootState) => Selected
type EqualityFn = (a: any, b: any) => boolean
export type DevModeCheckFrequency = 'never' | 'once' | 'always'
interface UseSelectorOptions {
equalityFn?: EqualityFn
devModeChecks?: {
stabilityCheck?: DevModeCheckFrequency
identityFunctionCheck?: DevModeCheckFrequency
}
}
const result: Selected = useSelector(
selector: SelectorFn,
options?: EqualityFn | UseSelectorOptions
)
Parameters
| Name | Description |
|---|---|
selector | A function that receives the entire Redux store state as its only argument, and returns the value this component needs from it. |
options? | Either an equality function used to compare selector results (such as shallowEqual), or an options object containing an equalityFn field and a devModeChecks field for configuring the development-mode checks below. |
Returns
The return value of the selector function, recalculated whenever the component renders with a new selector reference or a dispatched action changes the selected value.
Usage Guide
Selector behavior
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.
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.
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).
The selector is approximately equivalent to the mapStateToProps argument to connect conceptually.
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.
There are potential edge cases with using props in selectors that may cause issues. See the Usage Warnings section of this page for further details.
Equality Comparisons and Updates
When the function component renders, the provided selector function will be called and its result will be returned
from the useSelector() hook. (A cached result may be returned by the hook without re-running the selector if it's the same function reference as on a previous render of the component.)
However, when an action is dispatched to the Redux store, useSelector() only forces a re-render if the selector result
appears to be different than the last result. The default comparison is a strict === reference
comparison. This is different than connect(), which uses shallow equality checks on the results of mapState calls
to determine if re-rendering is needed. This has several implications on how you should use useSelector().
With mapState, all individual fields were returned in a combined object. It didn't matter if the return object was
a new reference or not - connect() just compared the individual fields. With useSelector(), returning a new object
every time will always force a re-render by default. If you want to retrieve multiple values from the store, you can:
- Call
useSelector()multiple times, with each call returning a single field value - Use Reselect or a similar library to create a memoized selector that returns multiple values in one object, but only returns a new object when one of the values has changed.
- Use the
shallowEqualfunction from React-Redux as theequalityFnargument touseSelector(), like:
import { shallowEqual, useSelector } from 'react-redux'
// Pass it as the second argument directly
const selectedData = useSelector(selectorReturningObject, shallowEqual)
// or pass it as the `equalityFn` field in the options argument
const selectedData = useSelector(selectorReturningObject, {
equalityFn: shallowEqual,
})
- Use a custom equality function as the
equalityFnargument touseSelector(), like:
import { useSelector } from 'react-redux'
// equality function
const customEqual = (oldValue, newValue) => oldValue === newValue
// later
const selectedData = useSelector(selectorReturningObject, customEqual)
The optional comparison function also enables using something like Lodash's _.isEqual() or Immutable.js's comparison capabilities.
Examples
Basic usage:
import React from 'react'
import { useSelector } from 'react-redux'
export const CounterComponent = () => {
const counter = useSelector((state) => state.counter)
return <div>{counter}</div>
}
Using props via closure to determine what to extract:
import React from 'react'
import { useSelector } from 'react-redux'
export const TodoListItem = (props) => {
const todo = useSelector((state) => state.todos[props.id])
return <div>{todo.text}</div>
}
Using memoizing selectors
When using useSelector with an inline selector as shown above, a new instance of the selector is created whenever the component is rendered. This works as long as the selector does not maintain any state. However, memoizing selectors (e.g. created via createSelector from reselect) do have internal state, and therefore care must be taken when using them. Below you can find typical usage scenarios for memoizing selectors.
When the selector does only depend on the state, simply ensure that it is declared outside of the component so that the same selector instance is used for each render:
import React from 'react'
import { useSelector } from 'react-redux'
import { createSelector } from 'reselect'
const selectNumCompletedTodos = createSelector(
(state) => state.todos,
(todos) => todos.filter((todo) => todo.completed).length,
)
export const CompletedTodosCounter = () => {
const numCompletedTodos = useSelector(selectNumCompletedTodos)
return <div>{numCompletedTodos}</div>
}
export const App = () => {
return (
<>
<span>Number of completed todos:</span>
<CompletedTodosCounter />
</>
)
}
The same is true if the selector depends on the component's props, but will only ever be used in a single instance of a single component:
import React from 'react'
import { useSelector } from 'react-redux'
import { createSelector } from 'reselect'
const selectCompletedTodosCount = createSelector(
(state) => state.todos,
(_, completed) => completed,
(todos, completed) =>
todos.filter((todo) => todo.completed === completed).length,
)
export const CompletedTodosCount = ({ completed }) => {
const matchingCount = useSelector((state) =>
selectCompletedTodosCount(state, completed),
)
return <div>{matchingCount}</div>
}
export const App = () => {
return (
<>
<span>Number of done todos:</span>
<CompletedTodosCount completed={true} />
</>
)
}
However, when the selector is used in multiple component instances and depends on the component's props, you need to ensure that selector's memoization behavior is properly configured (see here for details).
Development mode checks
useSelector runs some extra checks in development mode to watch for unexpected behavior. These checks do not run in production builds.
These checks were first added in v8.1.0
Selector result stability
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).
This is important, as a selector that returns a different result reference when called again with the same inputs will cause unnecessary rerenders.
// this selector will return a new object reference whenever called,
// which causes the component to rerender after *every* action is dispatched
const { count, user } = useSelector((state) => ({
count: state.count,
user: state.user,
}))
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.
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.
<Provider store={store} stabilityCheck="always">
{children}
</Provider>
function Component() {
const count = useSelector(selectCount, {
devModeChecks: { stabilityCheck: 'never' },
})
// run once (default)
const user = useSelector(selectUser, {
devModeChecks: { stabilityCheck: 'once' },
})
// ...
}
Identity Function (state => state) Check
This was previously referred to as noopCheck.
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.
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.
// BAD: this selector returns the entire state, meaning that the component will rerender unnecessarily
const { count, user } = useSelector((state) => state)
// GOOD: instead, select only the state you need, calling useSelector as many times as needed
const count = useSelector((state) => state.count.value)
const user = useSelector((state) => state.auth.currentUser)
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.
<Provider store={store} identityFunctionCheck="always">
{children}
</Provider>
function Component() {
const count = useSelector(selectCount, {
devModeChecks: { identityFunctionCheck: 'never' },
})
// run once (default)
const user = useSelector(selectUser, {
devModeChecks: { identityFunctionCheck: 'once' },
})
// ...
}
Comparisons with connect
There are some differences between the selectors passed to useSelector() and a mapState function:
- The selector may return any value as a result, not just an object.
- 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.
- The selector function does not receive an
ownPropsargument. However, props can be used through closure (see the examples above) or by using a curried selector. - You can use the
equalityFnoption to customize the comparison behavior
Usage Warnings
Stale Props and "Zombie Children"
The React-Redux hooks API has been production-ready since we released it in v7.1.0, and we recommend using the hooks API as the default approach in your components. However, there are a couple of edge cases that can occur, and we're documenting those so that you can be aware of them.
In practice, these are a rare concern - we've received far more comments about these being in the docs than actual reports of these being a real problem in an app.
One of the most difficult aspects of React Redux's implementation is ensuring that if your mapStateToProps function is defined as (state, ownProps), it will be called with the "latest" props every time. Up through version 4, there were recurring bugs reported involving edge case situations, such as errors thrown from a mapState function for a list item whose data had just been deleted.
Starting with version 5, React Redux has attempted to guarantee that consistency with ownProps. In version 7, that is implemented using a custom Subscription class internally in connect(), which forms a nested hierarchy. This ensures that connected components lower in the tree will only receive store update notifications once the nearest connected ancestor has been updated. However, this relies on each connect() instance overriding part of the internal React context, supplying its own unique Subscription instance to form that nesting, and rendering the <ReactReduxContext.Provider> with that new context value.
With hooks, there is no way to render a context provider, which means there's also no nested hierarchy of subscriptions. Because of this, the "stale props" and "zombie child" issues may potentially re-occur in an app that relies on using hooks instead of connect().
Specifically, "stale props" means any case where:
- a selector function relies on this component's props to extract data
- a parent component would re-render and pass down new props as a result of an action
- but this component's selector function executes before this component has had a chance to re-render with those new props
Depending on what props were used and what the current store state is, this may result in incorrect data being returned from the selector, or even an error being thrown.
"Zombie child" refers specifically to the case where:
- Multiple nested connected components are mounted in a first pass, causing a child component to subscribe to the store before its parent
- An action is dispatched that deletes data from the store, such as a todo item
- The parent component would stop rendering that child as a result
- However, because the child subscribed first, its subscription runs before the parent stops rendering it. When it reads a value from the store based on props, that data no longer exists, and if the extraction logic is not careful, this may result in an error being thrown.
useSelector() tries to deal with this by catching all errors that are thrown when the selector is executed due to a store update (but not when it is executed during rendering). When an error occurs, the component will be forced to render, at which point the selector is executed again. This works as long as the selector is a pure function and you do not depend on the selector throwing errors.
If you prefer to deal with this issue yourself, here are some possible options for avoiding these problems altogether with useSelector():
- Don't rely on props in your selector function for extracting data
- In cases where you do rely on props in your selector function and those props may change over time, or the data you're extracting may be based on items that can be deleted, try writing the selector functions defensively. Don't just reach straight into
state.todos[props.id].name- readstate.todos[props.id]first, and verify that it exists before trying to readtodo.name. - Because
connectadds the necessarySubscriptionto the context provider and delays evaluating child subscriptions until the connected component has re-rendered, putting a connected component in the component tree just above the component usinguseSelectorwill prevent these issues as long as the connected component gets re-rendered due to the same store update as the hooks component.
For a longer description of these scenarios, see:
Performance
As mentioned earlier, by default useSelector() will do a reference equality comparison of the selected value when running the selector function after an action is dispatched, and will only cause the component to re-render if the selected value changed. However, unlike connect(), useSelector() does not prevent the component from re-rendering due to its parent re-rendering, even if the component's props did not change.
If further performance optimizations are necessary, you may consider wrapping your function component in React.memo():
const CounterComponent = ({ name }) => {
const counter = useSelector((state) => state.counter)
return (
<div>
{name}: {counter}
</div>
)
}
export const MemoizedCounterComponent = React.memo(CounterComponent)
TypeScript
useSelector accepts a pre-typed setup via useSelector.withTypes<RootState>(), which lets you define the state type once instead of specifying it in every selector. See Usage with TypeScript for details on defining pre-typed hooks.
See Also
useSignalSelector: a fine-grained variant of this hook that tracks which parts of the state each selector readsuseDispatchanduseStore: the other core React Redux hooks- Hooks overview