Full answer
Building a component inside the full application makes it hard to reach edge states (loading, error, long text, empty list). Storybook gives each component its own workshop with a gallery of states.
Writing stories. A story file sits next to the component, such as Button.stories.jsx. It has a default export called meta, which describes the component (title, component, shared args, parameters), and named exports, one per story. In CSF3, a story is an object with args (the props), and optionally render, play and decorators:
import { Button } from './Button';
export default { title: 'Components/Button', component: Button, args: { children: 'Save' } };
export const Primary = { args: { variant: 'primary' } };
export const Disabled = { args: { disabled: true } };Why teams use it:
- Isolated development. Build and tweak a component with hot reload without logging in or navigating to the right screen.
- Documentation and a shared language. Autodocs generate prop tables from types and stories, so designers, developers and QA see the same catalogue (a living design system).
- Testing.
playfunctions simulate user interactions (using Testing Library utilities) and assert the result in the browser. The a11y addon runs axe on each story. Visual regression tools (Chromatic, Percy or similar) screenshot each story and detect unintended changes. Stories can be reused in Vitest tests by composing them. - Review. Pull requests can publish a Storybook build for review.
Practical points: use decorators to wrap stories in providers (theme, router, query client), mock network calls with MSW, keep stories small and focused on states, and avoid stories for every trivial component. Storybook adds configuration and maintenance, so it pays off most for shared component libraries and design systems.
The example is a button with stories, rendered here by a small script that merges meta args and story args as Storybook does.
function Button({ variant = 'secondary', disabled = false, children }) {
return (
<button className={'btn btn-' + variant} disabled={disabled}>
{children}
</button>
);
}
// Button.stories.jsx: a default export with metadata, and a named export per story (CSF3).
export default {
title: 'Components/Button',
component: Button,
args: { children: 'Save' },
};
export const Primary = { args: { variant: 'primary' } };
export const Secondary = {};
export const Disabled = { args: { disabled: true } };Storybook itself was not run. A small script rendered each story the way Storybook does, by merging meta args with story args, to check the examples. Output of the run: Disabled -> <button class="btn btn-secondary" disabled="">Save</button> Primary -> <button class="btn btn-primary">Save</button> Secondary -> <button class="btn btn-secondary">Save</button>
Key points
- Storybook develops and documents components in isolation, as a catalogue of states.
- Stories are CSF objects: a default meta export and named exports with args.
- Autodocs, interaction tests, accessibility checks and visual regression use the same stories.
- Decorators provide context (theme, router, data), and MSW mocks the network.
- It pays off most for shared libraries and design systems.
Common mistakes
- Writing stories that only show the default state.
- Leaving out decorators and getting components that crash without their providers.
- Letting stories drift from real usage because nobody runs them in CI.
- Creating a story for every tiny component and overloading maintenance.
- Calling Storybook a test tool without adding play functions or visual tests.
Follow-ups
- What is a play function and what does it let you test?
- How would you handle components that need a router or a data provider?
- How does visual regression testing with stories work?
Tips
- Describe stories as named states of a component.
- Mention the three uses: develop, document and test.
- Say it pays off most for shared component libraries.