Styling Strategies for React
Build, explain and debug React 19 interfaces: components, rendering, state, effects, identity, forms and actions, server components, concurrency, performance and testing, ending with architecture and typing work on real component challenges.
What you will be able to do
#- Compare CSS Modules, utility classes and CSS-in-JS.
- Compose conditional class names cleanly.
- Recognize the limits of inline styles.
Overview
#The options
React does not prescribe a styling method. The common choices are plain CSS with a naming convention, CSS Modules for local scoping, utility classes such as Tailwind, and CSS-in-JS libraries. Each trades scoping, colocation and runtime cost differently.
CSS Modules
CSS Modules scope class names to the file, so two components can use the same class name without clashing. You import the mapping and reference it in className.
import styles from './card.module.css';
export function Card() {
return <div className={styles.card}>...</div>;
}
Utility classes
Utility classes compose small single-purpose classes in the markup. They keep styles close to the element and avoid naming, at the cost of long className strings and a learning curve.
<div className="flex items-center gap-2 rounded border p-4">...</div>
Dynamic and conditional styles
Combine a base class with conditional classes. A small helper that joins truthy class names keeps the markup readable.
<div className={['btn', disabled && 'btn-disabled'].filter(Boolean).join(' ')}>...</div>
Inline styles
Inline styles are for values that must be computed at runtime, such as a measured width or a progress percentage. They cannot express pseudo-classes, media queries or keyframes, so use them sparingly.
Pitfall: runtime CSS-in-JS cost
Some CSS-in-JS libraries generate styles at runtime, which adds work to every render. Prefer solutions that extract styles at build time, or use CSS Modules and utility classes, when render cost matters.
Pitfall: global styles leaking
Plain global CSS leaks across components. Use CSS Modules, scoped utilities or a strict naming convention to keep styles local.
Choosing a strategy
Pick one primary approach per project. CSS Modules for scoped component styles, utility classes for rapid consistent styling, and inline styles only for runtime values.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
Can this inline style express a hover state?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.tsx<button style={{ color: 'red' }}>Go</button>Exercise 2Implement
Compose a button class name from a base class and a disabled modifier.
starter.tsxexport function Button({ disabled, children }: { disabled?: boolean; children: React.ReactNode }) { return <button>{children}</button>; }Reveal the reference answer
reference.tsxexport function Button({ disabled, children }: { disabled?: boolean; children: React.ReactNode }) { const className = ['btn', disabled && 'btn-disabled'].filter(Boolean).join(' '); return <button className={className} disabled={disabled}>{children}</button>; }Authored expected output: The button has the base class and the disabled modifier only when disabled.
The array holds the base class and a conditional modifier, and filter(Boolean) removes the false value before joining. This keeps the className readable and correct.
Exercise 3Debug
Two components both define a title class and interfere. Explain and fix.
starter.tsx// global.css .title { font-size: 20px; } // Card.tsx <h2 className="title">...</h2> // Panel.tsx <h2 className="title">...</h2>Reveal the reference answer
reference.tsx// card.module.css .title { font-size: 20px; } // Card.tsx import styles from './card.module.css'; <h2 className={styles.title}>...</h2>Authored expected output: Each component's title class is scoped and no longer clashes.
Global class names collide across components. CSS Modules rewrite the class to a unique name per file, so the two .title rules no longer affect each other.
Exercise 4Interview
Compare CSS Modules, utility classes and CSS-in-JS, and describe how you would choose.
starter.tsx// scoping, colocation, runtime costReveal the reference answer
reference.tsxCSS Modules scope names per file, so styles are local and you keep normal CSS syntax, at the cost of a separate file and no dynamic values. Utility classes colocate styling in the markup and enforce consistency, at the cost of long className strings and a learning curve. CSS-in-JS colocates styles with components and supports dynamic values, but runtime libraries add per-render cost and bundle size. I would choose based on the project: CSS Modules for scoped component styles, utilities for a design-system-driven codebase, and build-time CSS-in-JS or inline styles only for runtime values. The rule is one primary strategy per project to avoid inconsistency.Authored expected output: A comparison across scoping, colocation and runtime cost, plus a justified choice.
The answer should name the trade-offs and pick a strategy based on the project's needs, not declare one universally best.
Common misconceptions
#- React requires a specific styling library. React is agnostic; pick one strategy per project.
- Inline styles are as capable as CSS. They cannot express pseudo-classes, media queries or keyframes.
- Global CSS is fine at any scale. Global styles leak and become hard to change; scope them.
- Runtime CSS-in-JS is free. It adds work to each render; prefer build-time extraction when it matters.
Follow-up practice
#- react-animation
- react-component-architecture
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.