Performance: Effect execution timing
This affects how quickly side effects run after rendering, impacting interaction responsiveness and visual updates.
useEffect(() => {
fetchData();
}, []);useEffect(() => {
fetchData();
});| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| useEffect with empty deps | Minimal, runs once | Single reflow | Low paint cost | [OK] Good |
| useEffect without deps | Runs every render | Multiple reflows | High paint cost | [X] Bad |
| useLayoutEffect heavy work | Blocks DOM updates | Blocks reflow | Blocks paint | [X] Bad |
| useEffect heavy work | Runs after paint | No blocking reflow | Paint not blocked | [!] OK |
useEffect hook run its effect function by default?useEffect runs after every render, including the first render and all updates.useEffect only once when the component mounts?[] means the effect runs only once after the first render (mount).function Counter() {
const [count, setCount] = React.useState(0);
React.useEffect(() => {
console.log('Effect:', count);
return () => console.log('Cleanup:', count);
}, [count]);
return setCount(count + 1)}>Increment;
}React.useEffect(() => {
const id = setInterval(() => console.log('Tick'), 1000);
return clearInterval(id);
}, []);return () => clearInterval(id); to run cleanup on unmount.propA or stateB changes, but not on every render. Which dependency array is correct?React.useEffect(() => {
// effect code
}, ???);Pass the test and this pattern moves unknown