Performance: Conditional styling patterns
Conditional styling affects page rendering speed and visual stability by controlling how and when styles are applied during component updates.
import clsx from 'clsx'; const MyComponent = ({ isActive }) => { return <div className={clsx('base-style', { 'active-style': isActive })}>Hello</div>; }; // CSS: // .base-style { color: blue; font-weight: normal; } // .active-style { color: red; font-weight: bold; }
const MyComponent = ({ isActive }) => { return <div style={{ color: isActive ? 'red' : 'blue', fontWeight: isActive ? 'bold' : 'normal' }}>Hello</div>; };
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Inline style objects recreated each render | No extra DOM nodes | Multiple reflows per render | High paint cost due to style recalculations | [X] Bad |
| Conditional CSS class toggling | No extra DOM nodes | Single reflow on class change | Low paint cost leveraging browser optimizations | [OK] Good |
isActive inside a component's JSX?condition ? valueIfTrue : valueIfFalse is a common way to apply classes conditionally.<div className={isActive ? 'active' : ''}>Content</div> uses the ternary operator correctly. Options B and C use invalid JSX syntax. <div className={isActive && 'active' || 'inactive'}>Content</div> uses a logical expression that can cause unexpected results.isActive and isDisabled are booleans?className={`btn ${isActive && 'active'} ${isDisabled && 'disabled'}`} correctly uses template literals with logical AND. className='btn' + (isActive ? ' active' : '') + (isDisabled ? ' disabled' : '') uses string concatenation but misses curly braces for JSX expression. className={['btn', isActive && 'active', isDisabled && 'disabled'].join(' ')} uses an array join which works but is less common. className={btn isActive ? 'active' : '' isDisabled ? 'disabled' : ''} is invalid JSX syntax.isActive is false and isDisabled is true?
function Button({ isActive, isDisabled }) {
return (
<button className={`btn ${isActive ? 'active' : ''} ${isDisabled ? 'disabled' : ''}`}>Click</button>
);
}isActive is false, isActive ? 'active' : '' becomes an empty string. Since isDisabled is true, isDisabled ? 'disabled' : '' becomes 'disabled'.btn disabled (note the double space between 'btn' and 'disabled' due to empty string). This renders as class="btn disabled", which browsers treat as btn disabled.function Alert({ isError }) {
return <div className={isError ? 'error' : 'success'} else='hidden'>Message</div>;
}else attribute on elements. Conditional logic must be inside curly braces.else='hidden' leaves a valid ternary expression for className. To conditionally add 'hidden', use nested ternary or other logic inside className.else='hidden' because JSX does not support 'else' attribute -> Option D'highlight' should be added only if isSelected is true and count is greater than zero. Which of these is the best way to write the className attribute to handle this condition cleanly?className={`item ${isSelected && count > 0 ? 'highlight' : ''}`} uses a logical AND combined with ternary inside template literal, which can be confusing and may produce unexpected results. className={`item ${isSelected ? count > 0 ? 'highlight' : '' : ''}`} nests ternaries, which reduces readability. className={`item ${isSelected && count > 0 && 'highlight'}`} uses logical AND but if false returns false which renders as 'false' string in className. className={isSelected && count > 0 ? 'item highlight' : 'item'} uses a clear ternary outside template literal, returning full class strings, which is clean and safe.className={isSelected && count > 0 ? 'item highlight' : 'item'} clearly returns either 'item highlight' or 'item' string, avoiding false or empty strings in className, making it the best pattern for this condition.Pass the test and this pattern moves unknown