# Component-Driven Development

> A methodology for building UIs bottom-up by developing components in complete isolation before assembling them into pages.

Subject: React | Lesson 43 of 48 | 5 min read
Interactive lesson: https://codewithanimation.in/react/component-driven-dev
Previous: [Headless Component Libraries](https://codewithanimation.in/react/headless-ui.md) | Next: [Testing React](https://codewithanimation.in/react/testing-react.md)

## Building Bottom-Up

Historically, developers built pages "top-down." You would create a `/dashboard` route, then build the layout, then the sidebar, then finally the tiny buttons inside it.

**Component-Driven Development (CDD)** reverses this. You build the smallest, most atomic components first (buttons, inputs), test them thoroughly, then assemble them into larger components, and finally into pages.

## The Storybook Sandbox

Building a complex error state for a `CheckoutForm` can be annoying if you have to actually fill your cart and trigger a backend error just to see the UI.

Tools like **Storybook** provide an isolated sandbox. You can render your `CheckoutForm` in isolation and manually pass it an `error="Card declined"` prop to instantly see how it looks, without needing the rest of the application or backend to run.

## Writing Stories

In Storybook, you write 'stories' to capture the different states of your component.

For a `Button`, you might write a `Primary` story, a `Secondary` story, a `Disabled` story, and a `Loading` story.

This acts as both a development environment and living, interactive documentation for your entire team.

## Common mistake: Data Fetching in Isolation

The hardest part of CDD is dealing with components that are deeply coupled to your backend API or global state (like Redux or Zustand).

If a component internally calls `useQuery('/api/user')`, it will crash in Storybook because the API doesn't exist. To fix this, you either have to mock the API (using tools like MSW), or split your components into 'Container' (handles data) and 'Presentational' (just takes props) pieces.

## Tip: The Design System Handover

CDD is the bridge between Design and Engineering. A Storybook instance maps almost 1:1 with a Figma file.

When a designer updates the 'Primary Button' in Figma, an engineer updates the 'Primary Story' in Storybook. This keeps both systems in perfect sync.

## Try it: The Component Sandbox

Explore a mocked Storybook interface. On the left is the code defining the 'stories'. On the right is the isolated sandbox where you can visually tweak the component's props without running a full application.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/component-driven-dev

## Check yourself

### Question 1

What is the core philosophy of Component-Driven Development?

- A. Building the backend database structure before touching the UI.
- B. Building UIs from the bottom up: starting with atomic components and assembling them into complex pages.
- C. Writing CSS directly inside React components using styled-components.

Correct answer: B

CDD is about starting small. You perfect the atomic pieces (buttons, inputs) before you try to build massive page layouts.

### Question 2

Why use a tool like Storybook to develop components?

- A. It allows you to develop, test, and document components in complete isolation without running the main app.
- B. It automatically generates React code from Figma designs.
- C. It makes the components render faster in production.

Correct answer: A

Storybook is a sandbox. It lets you force hard-to-reach UI states (like error banners) instantly by just changing props, rather than clicking through your whole app to trigger them naturally.

### Question 3

What is a common challenge when trying to render a complex component in isolation (e.g., in Storybook)?

- A. The CSS styles won't load in isolation.
- B. The component might crash if it relies on a global context (like a Router or Data Fetcher) that isn't provided in the sandbox environment.
- C. React hooks like `useState` do not work outside of the main App tree.

Correct answer: B

Components that are tightly coupled to their environment (e.g., fetching data, reading router params) will crash in a pure sandbox unless you explicitly provide mocks or wrappers (decorators) for those dependencies.

## Remember this

- Component-Driven Development means building bottom-up (atoms to pages).
- Tools like Storybook provide an isolated sandbox for UI development.
- Stories document the different visual states of a single component.
- Developing in isolation makes it trivial to test hard-to-reach UI states (like loading or error states).
- Deeply coupled data-fetching components require API mocking (MSW) or a Presentational/Container split to work in isolation.
