0
0
NextJSframework~8 mins

State and hooks in client components in NextJS - Performance & Optimization

Choose your learning style9 modes available
Performance: State and hooks in client components
MEDIUM IMPACT
This affects interaction responsiveness and rendering speed of client components by managing state changes efficiently.
Updating state on every keystroke in a text input
NextJS
"use client";
import { useState, useRef } from 'react';

export default function Search() {
  const [query, setQuery] = useState('');
  const timeout = useRef(null);

  function handleChange(e) {
    clearTimeout(timeout.current);
    const value = e.target.value;
    timeout.current = setTimeout(() => setQuery(value), 300);
  }

  return <input defaultValue={query} onChange={handleChange} />;
}
Debounces state updates to reduce re-renders, improving input responsiveness and reducing CPU work.
📈 Performance GainReduces re-renders from every keystroke to once per 300ms pause, lowering INP delay.
Updating state on every keystroke in a text input
NextJS
"use client";
import { useState } from 'react';

export default function Search() {
  const [query, setQuery] = useState('');
  return <input value={query} onChange={e => setQuery(e.target.value)} />;
}
Updates state on every keystroke causing a re-render each time, which can slow down input responsiveness especially with complex components.
📉 Performance CostTriggers 1 re-render per keystroke, increasing INP delay on slow devices.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
State update on every keystrokeMany small DOM updatesTriggers 1 reflow per updateHigh paint cost due to frequent changes[X] Bad
Debounced state updateFewer DOM updatesSingle reflow after debounceLower paint cost[OK] Good
Multiple independent state variablesMultiple updates possibleMultiple reflows if updated separatelyModerate paint cost[!] OK
Combined state objectSingle update per changeSingle reflowLower paint cost[OK] Good
Rendering Pipeline
State changes trigger React to schedule a re-render of the component. React then recalculates the virtual DOM, diffs it, and updates the real DOM. This affects the Layout and Paint stages in the browser.
JavaScript Execution
Style Calculation
Layout
Paint
Composite
⚠️ BottleneckLayout and Paint stages become expensive if many re-renders happen rapidly.
Core Web Vital Affected
INP
This affects interaction responsiveness and rendering speed of client components by managing state changes efficiently.
Optimization Tips
1Avoid updating state on every single user input without throttling or debouncing.
2Group related state variables into a single state object to batch updates.
3Use React hooks wisely to minimize unnecessary re-renders and layout recalculations.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance issue when updating state on every keystroke in a client component?
AIt increases the initial page load time.
BIt causes the browser to download more JavaScript files.
CIt causes a re-render for every keystroke, slowing input responsiveness.
DIt prevents the component from rendering.
DevTools: Performance
How to check: Open DevTools > Performance tab > Record while interacting with the component (e.g., typing). Stop recording and inspect the flame chart for frequent React renders and layout events.
What to look for: Look for many small re-render events and layout recalculations clustered during input, indicating inefficient state updates.