Redux: What is a selector?
20 February 2023 (Updated 20 February 2023)
On this page
In a nutshell
A selector is a like a query into your state. It is a function that accepts the Redux store or a slice of the store and returns data based on that store.
This is what it looks like:
function TodoList() {
// This anonymous arrow function is a selector!
const todos = useSelector(state => state.todos)
}
Benefits
- Encapsulate logic for looking up values (e.g., completed todo items, number of items completed).
- Reuse same selection logic.
- Memoize the selectors so that expensive computations aren’t re-calculated.
Sources
Tagged:
Redux
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment