Skip to content
Learn Netverks

Lesson

Step 24/36 67% through track

conditional-styles

Conditional styles and variants

Last reviewed May 28, 2026 Content v20260528
Track mode
client_react
Means
In-browser React TSX
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Conditional styles and variants: the concepts, APIs, and habits you need before advancing in React.

Without Conditional styles and variants, you will struggle to read or extend React codebases and playground exercises.

You will apply Conditional styles and variants in contexts like: SPAs, dashboards, design-system-driven products, and React Native mobile apps.

Write TypeScript/TSX, click Run in browser—React 18 loads from CDN, JSX compiles in the tab, UI renders in the preview root, and printOutput feeds the terminal.

When you can explain the previous lesson's ideas without copying starter code.

Product UIs need variants: primary vs danger buttons, compact vs comfortable tables. In React you model variants as props and map them to class names or style tokens—keep the mapping in one place so designers and engineers share vocabulary.

Variant maps

const variantClass: Record<'primary' | 'ghost', string> = {
  primary: 'btn btn-primary',
  ghost: 'btn btn-ghost',
};

function Button({ variant = 'primary', children }) {
  return <button className={variantClass[variant]}>{children}</button>;
}

Boolean modifiers

Append modifier classes when flags are true: isActive && 'is-active'. For many flags, build an array and filter(Boolean).join(' ') to avoid double spaces.

Pitfalls

  • Recreating style objects inline every render can break memoized children—extract stable objects or use classes.
  • String concatenation typos silently fail—TypeScript unions for variant props catch mistakes early.

Self-check

  1. Why is a Record<Variant, string> better than three nested ternaries?
  2. How would you add a size="sm" | "md" prop alongside variant?

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs in your browser in a sandboxed frame. Backend runners appear when this track’s profile allows them.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Style from state how?
  • Accessibility check?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump