Of course! This is a very common and important goal in React and Redux development. The phrase "only one render" is a great way to think about it. The real goal is to prevent unnecessary re-renders.
A component should only re-render when the data it actually uses has changed.
Let's break down how to achieve this with Redux Toolkit.
The Golden Rule of Redux Renders
A component connected to Redux with the useSelector hook will re-render if and only if:
The value returned by your selector function is different (!==) from the last time it ran.
Understanding this rule is 90% of the battle. Almost every performance problem comes from breaking this rule.
How to Manage Renders: The Key Strategies
Here are the most effective strategies, from most important to advanced.
1. Select Only the Smallest Piece of State You Need
This is the #1 most important rule. Don't select a whole object if you only need one property from it.
❌ BAD: Selects the whole user object
If any property on the user object changes (like age or lastLogin), this component will re-render, even though it only displays the name.
jsx
// UserHeader.js
import { useSelector } from 'react-redux';
function UserHeader() {
// This selector returns the whole user object.
const user = useSelector((state) => state.user);
console.log('UserHeader is rendering...');
return <h1>Welcome, {user.name}!</h1>;
}
✅ GOOD: Selects only the name property
This component will only re-render if state.user.name changes. If state.user.age changes, this component will not re-render.
jsx
// UserHeader.js
import { useSelector } from 'react-redux';
function UserHeader() {
// This selector returns just the string value of the name.
const userName = useSelector((state) => state.user.name);
console.log('UserHeader is rendering...');
return <h1>Welcome, {userName}!</h1>;
}
2. Don't Create New Objects or Arrays Inside useSelector
The useSelector hook uses a strict reference equality check (===). If you create a new object or array inside your selector, it will be a new reference every single time, causing a re-render on any state change.
❌ VERY BAD: Creates a new object every time
This selector creates a new { name, email } object on every run. Since oldObject !== newObject is always true, this component will re-render every time any action is dispatched in your entire app.
✅ GOOD: Use multiple, simple selectors
Each selector returns a primitive value (string, number, boolean). React-Redux is smart enough to batch these, so it still only results in a single re-render if both name and email change at the same time.
3. Use Memoized Selectors for Derived Data (createSelector)
What if you need to compute something based on state? For example, getting a list of "done" todos.
❌ BAD: Creates a new array every time
This filter operation creates a new array every time, even if the todos haven't changed. This causes unnecessary re-renders.
✅ BEST: Use createSelector from the reselect library
Redux Toolkit automatically includes and re-exports reselect, so you can import createSelector directly from @reduxjs/toolkit.
A memoized selector only re-runs the calculation if its input state has changed.
Define the selector once, preferably in your slice file.
Import and use it in your component.
jsx
// features/todos/todosSlice.js
import { createSlice, createSelector } from '@reduxjs/toolkit';
// ... your createSlice code here ...
// --- SELECTORS ---
// 1. Define "input selectors" that just get a piece of state
const selectTodosItems = (state) => state.todos.items;
// 2. Create the memoized selector
export const selectDoneTodos = createSelector(
[selectTodosItems], // Input selectors
(items) => { // "Output selector" - the function that does the work
console.log('Computing done todos... (This will only run when items change!)');
return items.filter(todo => todo.completed);
}
);
jsx
// DoneTodosComponent.js
import { useSelector } from 'react-redux';
import { selectDoneTodos } from './features/todos/todosSlice';
function DoneTodosList() {
// Now, this selector is super efficient!
const doneTodos = useSelector(selectDoneTodos);
console.log('DoneTodosList is rendering...');
return (
<ul>
{doneTodos.map(todo => <li key={todo.id}>{todo.text}</li>)}
</ul>
);
}
Now, DoneTodosList will only re-render when the state.todos.items array actually changes. If you dispatch an action that changes state.user, this component will not re-render.
4. Use React.memo for Child Components
Sometimes a component re-renders because its parent re-rendered, not because of Redux. If you pass props to a child component, wrap it in React.memo to prevent it from re-rendering if its props haven't changed.
jsx
// ChildComponent.js
import React from 'react';
function UserAvatar({ userId, size }) {
console.log(`Rendering Avatar for user ${userId}`);
// ... render logic
return <div className={`avatar-${size}`}>Avatar for {userId}</div>;
}
// Wrap it in React.memo!
export default React.memo(UserAvatar);
Quick Summary & Checklist
To achieve minimal renders:
Be specific with useSelector: useSelector(state => state.user.name) is better than useSelector(state => state.user).
Use multiple useSelector hooks for multiple pieces of data instead of one selector that returns an object.
Use createSelector for any data that needs to be filtered, mapped, or computed. Define these selectors in your slice files.
Wrap child components in React.memo if they receive props and you want to prevent them from re-rendering when the parent does.
Use the Redux DevTools: They can show you exactly what state changed with each action, helping you trace why a component re-rendered.