Performance: What is state
State affects how React components update and re-render, impacting interaction responsiveness and rendering speed.
const [count, setCount] = useState(0); function increment() { setCount(prev => prev + 3); }
const [count, setCount] = useState(0); function increment() { setCount(count + 1); setTimeout(() => setCount(count + 2), 0); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Multiple separate state updates | Multiple DOM updates | Multiple reflows | Higher paint cost | [X] Bad |
| Single batched state update | Minimal DOM updates | Single reflow | Lower paint cost | [OK] Good |
state in a React component?count with initial value 0 in a React functional component?import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
Count: {count}
setCount(count + 1)}>Increase
);
}const [count, setCount] = useState(0);
function increment() {
count = count + 1;
}const [items, setItems] = useState([]); and update with setItems([...items, newItem]) uses spread syntax to create a new array and calls setter, which is correct. Options A and D mutate the array directly. Use const items = []; and reassign with items = items.concat(newItem) tries to reassign a const variable.Pass the test and this pattern moves unknown