In this lesson, we’ll learn how to:
These techniques help your app stay fast, efficient, and scalable.
Every time a Redux state changes:
This becomes problematic when:
So, we optimize to: ✅ Reduce wasted renders ✅ Improve responsiveness ✅ Scale the UI smoothly
A selector is a function that reads data from your Redux store:
const selectPokemon = (state) => state.pokemon.items;
But sometimes, we need derived data, such as filtered or sorted lists. If we compute derived data inside components, it re-computes on every render, even if data didn’t change.
reselectReselect allows us to cache derived values using memoization.
Install:
npm install reselect
Create a selector:
import { createSelector } from "reselect";const selectPokemon = (state) => state.pokemon.items;export const selectFilteredPokemon = createSelector([selectPokemon],(items) => items.filter((p) => p.name.startsWith("a")));
items hasn’t changed → cached result is reusedEven with Redux Toolkit, re-renders can happen when:
const name = useSelector((state) => state.user.name);
Instead of:
const user = useSelector((state) => state.user);
useCallbackconst handleClick = useCallback(() => {dispatch(fetchPokemon());}, [dispatch]);
Another key optimization technique is splitting components so only the part that depends on Redux state re-renders.
Instead of:
function PokemonList() {const { items, status } = useSelector((state) => state.pokemon);return (<div><p>Status: {status}</p><ul>{items.map((p) => (<li key={p.name}>{p.name}</li>))}</ul></div>);}
Split into smaller components:
function PokemonStatus() {const status = useSelector((state) => state.pokemon.status);return <p>Status: {status}</p>;}function PokemonListItems() {const items = useSelector((state) => state.pokemon.items);return (<ul>{items.map((p) => (<li key={p.name}>{p.name}</li>))}</ul>);}
Now:
status changes → only PokemonStatus updatesitems changes → only PokemonListItems updates| Technique | Benefit |
|---|---|
| Reselect memoized selectors | Avoid unnecessary recomputation |
| Read minimal state with useSelector | Reduce component re-renders |
| Component splitting | Update only the parts of UI that need updating |
Performance optimization ensures your app stays smooth and scalable as it grows.