How to Implement a Storybook in React Applications

By Shiva Gaur
June 20, 2019 | 658 Views

Latest Industry insights to keep you updated on the latest happenings.

Storybook is the world’s best and popular UI component development tool among developers. It helps developers to design and develop UI components in an isolated environment. It runs in an isolation environment to the main application so the developer would not have to worry about the dependencies.

Getting Started by creating a new app:

Open the terminal and run the following commands:

  1. Create an application using create-react-app:

npm or yarn create the react-app application

  1. Move to application directory:

cd application

Getting started with React storybook:

Add the following dependencies to the dev-dependencies as we only need the storybook modules during development and not required at run time.

“devDependencies”: {

   “@babel/core”: “^7.2.2”,

   “@storybook/addon-actions”: “^3.3.13”,

   “@storybook/addon-info”: “^4.0.0”,

   “@storybook/addon-links”: “^3.3.13”,

   “@storybook/react”: “^3.3.13”,

   “babel-loader”: “^8.0.5”

}

Add the following script inside the package.json file:

"script": {

  "storybook": "start-storybook -p 9001 -s public",

}

Setting up storybook in your Project:

  1. Create a .storybook directory in your root directory.

  1. Create a config.js file.This file will check all the src directory file with extension `filename.stories.js`

3. To add the external cdn or bootstrap links to your storybook create a file name as preview-head.html and link your css and cdn links.

4. To add some addon to storybook register those add-ons in the addons.js file in the .config directory

5. Create a button component inside the src/components directory.

6. Create a button component and its story in the src/components/button directory.

This will create three isolation button stories one for default button, one button with a primary color and one with danger color.

Now to see the component on the storybook run `npm or yarn run storybook`.

Open chrome and with http:localhost:9001

To know more about the coding part, you can refer our GitHub repository.

RECOMMENDED

Monthly industry insights to keep you updated on latest happenings

Follow us on Twitter
Follow us on Facebook
Follow us on Linkedin