Performance: State synchronization patterns
MEDIUM IMPACT
This affects how fast the UI updates and how smoothly user interactions feel by managing state changes efficiently.
import { signal } from '@preact/signals'; const countSignal = signal(0); function ComponentA() { return <button onClick={() => countSignal.value++}>Increment</button>; } function ComponentB() { return <div>Count: {countSignal.value}</div>; }
function ComponentA() { const [count, setCount] = useState(0); return <button onClick={() => setCount(count + 1)}>Increment</button>; } function ComponentB() { const [count, setCount] = useState(0); return <div>Count: {count}</div>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Separate local states in multiple components | Multiple independent updates | Multiple reflows per update | High paint cost due to repeated layout | [X] Bad |
| Shared reactive state (signals or context) | Single update propagates to dependents | Single reflow per update | Lower paint cost with batched updates | [OK] Good |