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

ThemeProvider and Design Tokens

Wrapping your app in ThemeProvider to access theme values in any styled component: colors, spacing, breakpoints.

ThemeProvider and Design Tokens

You'll learn how to wrap your app in a ThemeProvider to make design tokens accessible throughout your styled components.

The Central Theme Store

Instead of hardcoding colors, spacing, and breakpoints in every styled component, you can define them once in a theme object and inject them everywhere. The ThemeProvider from styled-components wraps your app and makes this theme available to every styled component through props.

Think of it like a shared design language: rather than each component saying "use #3B82F6," they all reference theme.colors.primary. Change the theme once, and every component updates.

How It Works

import { ThemeProvider } from 'styled-components';

const theme = {
  colors: {
    primary: '#3B82F6',
    secondary: '#10B981',
    text: '#1F2937',
  },
  spacing: {
    sm: '8px',
    md: '16px',
    lg: '24px',
  },
  breakpoints: {
    mobile: '768px',
    desktop: '1024px',
  },
};

function App() {
  return (
    <ThemeProvider theme={theme}>
      <YourComponents />
    </ThemeProvider>
  );
}

Now any styled component can access these values:

const Button = styled.button`
  background: ${props => props.theme.colors.primary};
  padding: ${props => props.theme.spacing.md};

  @media (min-width: ${props => props.theme.breakpoints.desktop}) {
    padding: ${props => props.theme.spacing.lg};
  }
`;

The theme flows down through React context—every styled component automatically receives it as a prop.

Key Takeaway: ThemeProvider centralizes your design tokens (colors, spacing, breakpoints) in one object, making them accessible via props.theme in any styled component—enabling consistent, maintainable design systems.