React Performance Trap: useContext Re-renders All Consumers

Nobody talks about this React performance trap — and it bit me hard in production. I was using useContext to manage auth + theme + cart state in a mid-size app. Only a few components actually used the cart state. But every time the cart updated — every single component consuming the context re-rendered. Even the ones that only needed the user's name. Here's why: React's Context API re-renders all consumers whenever the context value reference changes — regardless of which slice of state they actually use. There are workarounds with Context: → Split into multiple contexts (AuthContext, CartContext, ThemeContext separately) → Wrap consumers in React.memo() → Use useMemo to stabilize the context value But honestly? All of these feel like duct tape. This is exactly where Zustand and Redux Toolkit shine. Both let you subscribe to only the slice of state you need. A component using useSelector(state => state.user.name) will only re-render when that specific value changes — not when the cart updates, not when the theme toggles. ZUSTAND Minimal boilerplate, great for small-to-mid apps. Selector-based subscriptions built in. REDUX TOOLKIT Structured, scalable. useSelector memoizes out of the box with Reselect. The lesson I took away: useContext is great for low-frequency, global state (auth, theme, locale). The moment you have state that changes often and is consumed by many components — reach for a proper state manager. Performance issues in React are rarely obvious until they're in production. Knowing why re-renders happen matters more than memorizing which hook to use. Have you run into this? Would love to hear how you solved it — Context splitting, Zustand, RTK, or something else entirely? #ReactJS #JavaScript #WebDevelopment #Frontend #Zustand #Redux

Context can be optimized to avoid those re-renders, but at that point, you're basically building a DIY state manager. I’d rather just use Zustand and get selectors for free. Why fight the tool when you can just use one built for the job

Like
Reply

To view or add a comment, sign in

Explore content categories