0
0
NextJSframework~8 mins

Zustand for client state in NextJS - Performance & Optimization

Choose your learning style9 modes available
Performance: Zustand for client state
MEDIUM IMPACT
This affects how fast the page responds to user actions and how efficiently the UI updates without unnecessary delays.
Managing client state efficiently in a Next.js app
NextJS
import { create } from 'zustand';

const useStore = create(set => ({
  count: 0,
  increment: () => set(state => ({ count: state.count + 1 }))
}));

function Counter() {
  const count = useStore(state => state.count);
  const increment = useStore(state => state.increment);
  return (
    <div>
      <button onClick={increment}>Increment</button>
      <p>{count}</p>
    </div>
  );
}

export default Counter;
Zustand updates only components that use the changed state slice, reducing re-renders and improving responsiveness.
📈 Performance GainSingle re-render per state change, improving INP and reducing CPU usage.
Managing client state efficiently in a Next.js app
NextJS
import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);
  return (
    <div>
      <button onClick={() => setCount(count + 1)}>Increment</button>
      <p>{count}</p>
    </div>
  );
}

export default Counter;
Using local useState in many components causes repeated state duplication and unnecessary re-renders across unrelated parts.
📉 Performance CostTriggers multiple re-renders and can cause slower interaction responsiveness as app scales.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Local useState in many componentsMultiple updates across componentsMultiple reflows per updateHigher paint cost due to many re-renders[X] Bad
Zustand with selective subscriptionsMinimal updates only where neededSingle reflow per updateLower paint cost with fewer re-renders[OK] Good
Rendering Pipeline
Zustand updates state outside React's local state, then triggers selective React component re-renders only where state is used.
JavaScript Execution
React Reconciliation
Paint
Composite
⚠️ BottleneckReact Reconciliation when many components re-render unnecessarily
Core Web Vital Affected
INP
This affects how fast the page responds to user actions and how efficiently the UI updates without unnecessary delays.
Optimization Tips
1Use Zustand selectors to subscribe only to needed state slices.
2Avoid updating global state unnecessarily to reduce re-renders.
3Profile React components to ensure minimal re-render on state changes.
Performance Quiz - 3 Questions
Test your performance knowledge
How does Zustand improve interaction responsiveness compared to local useState?
ABy batching all state updates into one global update
BBy updating only components subscribed to changed state slices
CBy forcing full page reload on state change
DBy storing state on the server instead of client
DevTools: React DevTools Profiler
How to check: Record interactions and observe component re-render counts and durations when state changes.
What to look for: Look for fewer component re-renders and shorter render times indicating efficient state updates.