Automatic setup

Note: be sure you have a in your project or the above command will fail.

If you want to set up Storybook manually for your React project, this is the guide for you.

Step 1: Add dependencies

Add @storybook/react to your project. To do that, run:

  1. npm install @storybook/react --save-dev

Add react, react-dom, @babel/core, and babel-loader

Then add the following NPM script to your package.json in order to start the storybook later in this guide:

  1. {
  2. "storybook": "start-storybook"
  3. }
  4. }

Step 3: Create the config file

For a basic Storybook configuration, the only thing you need to do is tell Storybook where to find stories.

To do that, create a file at .storybook/config.js with the following content:

Now create a ../src/index.stories.js file, and write your first story like this:

  1. import React from 'react';
  2. export default { title: 'Button' };
  3. export const withText = () => <Button>Hello Button</Button>;
  4. export const withEmoji = () => (
  5. <Button><span role="img" aria-label="so cool">😀 😎 👍 💯</span></Button>
  6. );

Each story is a single state of your component. In the above case, there are two stories for the demo button component:

Finally: Run your Storybook

Now everything is ready. Run your storybook with:

Now you can develop your components and write stories and see the changes in Storybook immediately since it uses Webpack’s hot module reloading.