Redux - DevTools & Performance Patterns
Learn how to avoid unnecessary renders by selecting minimal state and using memoized selectors, and understand how DevTools help inspect actions and state changes.
Try it: Renders vs. selection
View source
const { createSlice, configureStore } = RTK;
const slice = createSlice({ name:'stats', initialState:{ a:0, b:0 }, reducers:{ incA(s){s.a++}, incB(s){s.b++} } });
const store = configureStore({ reducer:{ stats: slice.reducer } });
function App(){
const { Provider, useSelector, useDispatch } = ReactRedux;
let rendersA=0, rendersB=0;
function ShowA(){ const a = useSelector(s=>s.stats.a); rendersA++; return <div>A:{a} (renders {rendersA})</div> }
function ShowB(){ const b = useSelector(s=>s.stats.b); rendersB++; return <div>B:{b} (renders {rendersB})</div> }
function Panel(){
const dispatch = useDispatch();
return (
<div>
<button onClick={()=>dispatch(slice.actions.incA())}>Inc A</button>
<button onClick={()=>dispatch(slice.actions.incB())} style={{marginLeft:8}}>Inc B</button>
<div style={{marginTop:8}}><ShowA /><ShowB /></div>
</div>
);
}
return <Provider store={store}><Panel /></Provider>;
}
ReactDOM.createRoot(document.getElementById('perf-root')).render(<App />);
Tips
- Select the smallest slice of state needed in each component.
- Use memoized selectors to avoid expensive recalculations.
- In DevTools, watch actions, diffs, and state time-travel to reproduce bugs.