Performance: Updating phase
This affects how quickly React updates the UI after state or props change, impacting interaction responsiveness and visual stability.
function Search() { const [query, setQuery] = React.useState(''); const [debouncedQuery, setDebouncedQuery] = React.useState(''); React.useEffect(() => { const handler = setTimeout(() => setDebouncedQuery(query), 300); return () => clearTimeout(handler); }, [query]); function handleChange(e) { setQuery(e.target.value); } return <input value={query} onChange={handleChange} />; }
function Search() { const [query, setQuery] = React.useState(''); function handleChange(e) { setQuery(e.target.value); } return <input value={query} onChange={handleChange} />; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Frequent state updates on every input | Many small DOM updates | Many reflows per keystroke | High paint cost due to frequent changes | [X] Bad |
| Debounced state updates | Fewer DOM updates | Single reflow per debounce interval | Lower paint cost | [OK] Good |
| No memoization on large lists | All child nodes updated | Many reflows | High paint cost | [X] Bad |
| Memoized child components | Minimal DOM updates | Few reflows | Low paint cost | [OK] Good |
updating phase in a React component?useState, e.g., setCount.setCount(count + 1) correctly updates the state by adding 1.const [count, setCount] = useState(0);
useEffect(() => {
console.log('Count changed:', count);
}, [count]);
setCount(1);
setCount(2);count changes, so it logs only once with the final value 2.const [count, setCount] = useState(0);
function increment() {
count = count + 1;
setCount(count);
}count variable, which is not allowed in React state management.setCount(count + 1) without modifying count directly.function MyComponent({ value }) {
const [state, setState] = useState(value);
// What should go here?
}value prop changes, use useEffect with [value] dependency.value changes, preventing infinite loops or extra renders.Pass the test and this pattern moves unknown