CodeBlanks Logo Icon CodeBlanks

Summary - styled-components

Write a themed button family (primary, ghost) with styled-components.

Summary - styled-components

In this group you entered the CSS-in-JS world. styled-components embeds styles into JS; you write CSS in a template literal and get a component.

Props give dynamic styles; a theme gives centralized ones. Dark/light mode is easy with a theme. All styles are isolated and clash-free.

For a consistent design language across a project, the theme approach is a powerful tool.

Practice styled summary task

Complete the skeleton. Create a styled component DangerButton defined with styled.button\`color: ${props => props.color};\`. Return <DangerButton color="red">Delete</DangerButton>. Export it.

Starter code

App.jsx
// Define DangerButton here

Sample solution

App.jsx
const DangerButton = styled.button`color: ${props => props.color};`;

function App() { return <DangerButton color="red">Delete</DangerButton>; }
export default DangerButton;

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which is wrong?

App.jsx
A theme centralizes colors.
  • styled.button`...` makes a component
  • a theme is useless and never used Correct answer
  • ThemeProvider spreads the theme
  • props give dynamic styles

Explanation: A theme is a powerful tool for consistent design.

2 Summary - styled-components: match the terms

Term Meaning
styled.button Makes a component
${props => ...} Dynamic value
ThemeProvider Spreads theme
props.theme Theme access

All lessons in Beginner