React & Modern Frontend (445 Qs)•Part 7: Supported Libraries, Styling & Integrations (139 - 151)
What is reselect and how it works?
Reselect is a selector library (for Redux) which uses memoization concept. It was originally written to compute derived data from Redux-like applications state, but it can't be tied to any architecture or library.
Reselect keeps a copy of the last inputs/outputs of the last call, and recomputes the result only if one of the inputs changes. If the same inputs are provided twice in a row, Reselect returns the cached output. It's memoization and cache are fully customizable.
Practical Code Example
javascript
// Reselect: Memoized selector library preventing unnecessary re-computations
import { createSelector } from 'reselect';
const selectItems = (state) => state.cart.items;
export const selectCartTotal = createSelector(
[selectItems],
(items) => items.reduce((sum, item) => sum + item.price, 0) // Memoized!
);