Component Documentation with Storybook
Why Develop Components in Isolation
Building and checking a component only within your full app means navigating through several screens just to see one small piece, and makes it hard to see all its variations (like a Button's default, disabled, and loading states) side by side. Storybook renders each component on its own, showing every documented state directly.
Note: This is especially valuable for a shared component library used across many parts of an app or by multiple teams.
Warning: Storybook is a separate npm package and its own dev server/build tool — it can't be demonstrated running inside this CDN-only preview.
Example: Why Develop Components in Isolation
// Run in your local React project (npm install required)
// Button.stories.js
import Button from './Button';
export default { component: Button };
export const Default = { args: { children: 'Click me' } };
export const Disabled = { args: { children: 'Disabled', disabled: true } };
Writing a Story
A story is a small object describing one specific way to render a component — usually just a set of args (props) to pass it. Storybook automatically renders each exported story as its own browsable entry in its UI, letting you see and interact with that exact variation.
Note: Name stories after the state they represent (Default, Loading, Disabled, WithLongText) so they're self-documenting in Storybook's sidebar.
Warning: A component whose actual props have changed but whose stories weren't updated to match can show misleading or broken examples in Storybook.
Example: Writing a Story
// Run in your local React project (npm install required)
// Card.stories.js
import Card from './Card';
export default { component: Card };
export const WithImage = {
args: { title: 'Mountain View', imageUrl: '/mountain.jpg' },
};
export const NoImage = {
args: { title: 'No Image Provided' },
};
Storybook vs. Automated Tests
Storybook is primarily a visual development and documentation tool — a great way to browse and manually verify a component's states. It's not a replacement for automated tests like Jest/Testing Library, which programmatically verify behavior without a human looking at each state.
Note: Some teams combine the two: Storybook stories for visual review, plus separate automated tests asserting the same states behave correctly.
Warning: Relying solely on manually eyeballing Storybook stories, with no automated tests, means regressions can slip through unnoticed between reviews.
Example: Storybook vs. Automated Tests
// Run in your local React project (npm install required)
// Storybook: for visually browsing states
// Button.stories.js -> shows Default, Disabled, Loading visually
// Testing Library: for automated verification
// Button.test.js
test('disabled button cannot be clicked', () => {
render(<Button disabled>Click</Button>);
expect(screen.getByRole('button')).toBeDisabled();
});
- Writing stories that don't cover a component's meaningful prop variations, missing the point of documenting its different states.
- Letting stories go stale by not updating them when the component's actual props change.
- Treating Storybook as a replacement for real tests, when it's primarily a documentation and visual-development tool.
- Storybook is a tool for developing and documenting UI components in isolation, outside of a full running app.
- A story defines one specific rendered state of a component, like a Button with different variants.
- Storybook makes it easy to visually browse every state of every component without navigating the whole app to reach them.
- It pairs well with, but doesn't replace, automated tests like those written with Jest/Testing Library.
Requires npm install storybook — runs its own separate dev server, not available in this CDN-only sandbox.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Error Boundaries
- React Portals
- Modals using Portals (practical use)
- React Suspense
- Code Splitting with React.lazy
- Introduction to Server Components
- Introduction to Next.js (server-side React)
- Using React with TypeScript
- Scalable Folder Architecture
- Common React Design Patterns
- Component Documentation with Storybook
- Accessibility (a11y) in React
- i18n with react-i18next
- React Security Best Practices