Performance: Separation of concerns
MEDIUM IMPACT
Separation of concerns affects how easily the browser can update and render components by organizing code into focused parts.
const useCountStyle = (count) => React.useMemo(() => ({ color: count > 5 ? 'red' : 'blue', fontWeight: 'bold' }), [count]); function MyComponent() { const [count, setCount] = React.useState(0); const style = useCountStyle(count); return <div style={style} onClick={() => setCount(count + 1)}>Count: {count}</div>; }
function MyComponent() { const [count, setCount] = React.useState(0); const style = { color: count > 5 ? 'red' : 'blue', fontWeight: 'bold' }; return <div style={style} onClick={() => setCount(count + 1)}>Count: {count}</div>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Mixed logic and styles inline | High - recalculates on every state change | Multiple reflows per update | High paint cost due to style recalculation | [X] Bad |
| Memoized styles and separated logic | Low - only updates when needed | Single reflow per relevant change | Lower paint cost with stable styles | [OK] Good |