Performance: Separation of concerns
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 |
separation of concerns mean in React development?function UserName({ name }) {
return <h1>User: {name}</h1>;
}
function UserLogic() {
const user = { name: 'Anna' };
return <UserName name={user.name} />;
}function Profile() {
const [user, setUser] = React.useState(null);
React.useEffect(() => {
fetch('/api/user')
.then(res => res.json())
.then(data => setUser(data));
}, []);
return <div>{user.name}</div>;
}function Dashboard() {
const [data, setData] = React.useState(null);
React.useEffect(() => {
fetch('/api/data')
.then(res => res.json())
.then(json => setData(json));
}, []);
return <div>{data ? <Display data={data} /> : 'Loading...'}</div>;
}Pass the test and this pattern moves unknown