Skip to content
tezvyn:

Memoized Selectors: Avoid Needless Re-renders

Source: reselect.js.orgHardHow cards are made

A memoized selector caches its result, re-running only if its inputs from the Redux store change. Use it to compute derived data like a filtered list without triggering needless re-renders.

Why it exists

In a Redux application, any state change notifies all connected components. If a component calculates derived data (like filtering a list) directly, it will re-calculate on every single state update, even unrelated ones. This is inefficient and causes unnecessary component re-renders.

The mental model

A memoized selector is like a gatekeeper for derived data. It takes small, specific slices of the Redux state as inputs. It runs a calculation once and caches the result. On subsequent calls, it first checks if its inputs have changed. If not, it returns the cached result instantly. Crucially, it returns the exact same object reference, which React's shallow comparison (===) sees as unchanged, preventing a re-render.

How it works

Libraries like Reselect provide a createSelector function. You give it one or more "input selectors" (simple functions that extract pieces of state) and a "result function" that performs the computation. This creates a new, memoized selector. When this new selector is called with the global state, it runs the input selectors, compares their results to the previous call, and only executes the result function if an input has changed.

When to use it

Use memoized selectors whenever you compute derived data from the Redux state that will be used in a component. This is ideal for filtering a list of items, aggregating data like a shopping cart total, or transforming data into a specific shape for the UI. It prevents expensive computations and unnecessary re-renders.

When not to use it

Avoid selectors for simple state lookups that involve no computation (e.g., state => state.user.name), as the overhead is not justified. Also, if the input data is guaranteed to change on every render anyway, a selector provides no benefit as its cache would be constantly invalidated.

One canonical example

In a todo app, you have a list of todos and a visibility filter. A selector can take state.todos and state.visibilityFilter as inputs and return the filtered list. If you mark a todo complete, the state.todos input changes, the selector re-runs, and the component gets a new list. But if some unrelated state changes (like a loading status), the selector's inputs remain the same, it returns the cached list, and the component avoids a pointless re-render.

Interview question

When an unrelated part of the Redux state changes, how does a memoized selector prevent unnecessary component re-renders?

  • a.It re-computes the derived data but only notifies the component if the result is different.
  • b.It returns the exact same cached object reference, which React's shallow comparison sees as unchanged.Correct
  • c.It returns a new, identical object reference, which React optimizes away.
  • d.It automatically unsubscribes the component from state updates.
Why?

Memoized selectors prevent re-renders by returning the identical, cached object reference if their inputs haven't changed. React's shallow comparison (===) then perceives the prop as unchanged, thus avoiding a re-render. Option C is incorrect because returning a *new* object, even if content-identical, would still trigger a re-render due to reference inequality.

Just read this? Test yourself on what you have been reading.

Read the original → reselect.js.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on react — each one lists the topics its interview covers.

See open roles