Course contentsShow
← React
Lesson 1924 of 2,39644. Styling ApproachesFree lesson

Passing Props to Styled Components

Using props in template literals to create dynamic styles: `${props => props.primary ? 'blue' : 'gray'}` patterns.

Passing Props to Styled Components

You'll learn how to create dynamic styles by using component props inside styled-components template literals.

The Core Idea

Styled-components aren't just static styles—they're functions that receive your component's props. Inside the template literal, you can interpolate a function that accesses props and returns different CSS values based on those props. This is the key to creating truly dynamic, reusable styled components.

const Button = styled.button`
  background-color: ${props => props.primary ? 'blue' : 'gray'};
  color: white;
  padding: 10px 20px;
`;

// Usage
<Button primary>Primary Button</Button>
<Button>Default Button</Button>

How It Works

The pattern ${props => ...} creates a function that styled-components calls with your component's props. Think of it like a mini-computed style—every time React renders your component, styled-components evaluates these functions and generates the appropriate CSS.

You can use this pattern for any CSS property:

const Container = styled.div`
  width: ${props => props.fluid ? '100%' : '1200px'};
  margin: ${props => props.centered ? '0 auto' : '0'};
  opacity: ${props => props.disabled ? 0.5 : 1};
`;

The props are the same props you pass to the component in JSX—styled-components forwards them automatically. This creates a clean, declarative API where your component's visual state is controlled through familiar React patterns.

Key Takeaway: Use ${props => ...} inside styled-components template literals to create dynamic styles that respond to component props, turning static CSS into conditional, data-driven styling logic.