Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • React Tutorial
  • React Exercise
  • React Basic Concepts
  • React Components
  • React Props
  • React Hooks
  • React Router
  • React Advanced
  • React Examples
  • React Interview Questions
  • React Projects
  • Next.js Tutorial
  • React Bootstrap
  • React Material UI
  • React Ant Design
  • React Desktop
  • React Rebass
  • React Blueprint
  • JavaScript
  • Web Technology
Open In App
Next Article:
Survey Website using ReactJS
Next article icon

Storybook Using React

Last Updated : 22 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Storybook is an open-source tool to build the UI components individually, allowing developers to concentrate on one component at a time, examine it more deeply, and record its behavior and changes. The objective of the project is to increase the effectiveness of the development of React components by giving them a dedicated space where they can be systematically and interactively created, imaged, or documented.

Prerequisites

  • Node JS and npm
  • React
  • Storybook

Approach to use Storybook in React

  • To develop react components in isolation to allow for targeted and efficient development.
  • Create an interactive demo of React components that allows developers and designers to interact with them individually.
  • Directly within Storybook, you can document component behavior, use cases, and changes.
  • Early on in the product development process, Visual testing of components is carried out to determine if UI issues are present.
  • Displays a variety of characteristics and differences in the components, e.g. various props or user interactions.

Steps to Create the React App:

Step 1: Initialized a react app using the following command.

npx create-react-app react-storybook
cd react-storybook

Step 2: Install the required dependencies.

npx sb init

You must create a Storybook directory in the Project root, configure Storybook settings.Develop stories about each component of React, including descriptions of various states and use cases.

Project Structure:

Screenshot-(1419)

Note:

  • we will have to make two files of each component and place the files in their respective folder (components and stories). As you can see the above project structure you will see Button.js in components folder and Button.stories.js in stories folder.
  • In components folder, we have the proper structure of the components and their customizations
  • In stories folder, we will have interactions of storybook with each of the component and customize it accordingly.

The updated dependencies in package.json file will look like:

"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"devDependencies": {
"@storybook/addon-essentials": "^7.6.7",
"@storybook/addon-interactions": "^7.6.7",
"@storybook/addon-links": "^7.6.7",
"@storybook/addon-onboarding": "^1.0.10",
"@storybook/blocks": "^7.6.7",
"@storybook/preset-create-react-app": "^7.6.7",
"@storybook/react": "^7.6.7",
"@storybook/react-webpack5": "^7.6.7",
"@storybook/test": "^7.6.7",
"eslint-plugin-storybook": "^0.6.15",
"prop-types": "^15.8.1",
"storybook": "^7.6.7",
"webpack": "^5.89.0"
}

Example code (Button component):

JavaScript
// Button.js import PropTypes from "prop-types";  function Button({ 	label, 	backgroundColor = "red", 	size = "md", 	handleClick, 	color = "white", }) { 	let scale = 1; 	if (size === "sm") scale = 0.75; 	if (size === "lg") scale = 1.5; 	const style = { 		backgroundColor, 		padding: `${scale * 0.5}rem ${scale * 1}rem`, 		border: "none", 		color, 	}; 	return ( 		<button onClick={handleClick} style={style}> 			{label} 		</button> 	); }  Button.propTypes = { 	label: PropTypes.string, 	backgroundColor: PropTypes.string, 	size: PropTypes.oneOf(["sm", "md", "lg"]), 	handleClick: PropTypes.func, }; 
JavaScript
//Button.stories.js  import React from "react"; import Button from "../components/Button";  export default { 	title: "Button", 	component: Button, 	// this will show in action tab 	argTypes: { handleClick: { action: "handleClick" } },  };  export const Primary = () => ( 	<Button label="Primary Button" backgroundColor="blue" /> ); export const Secondary = () => <Button label="Secondary Button" />;  // this is used for making interactive componets in storybook const Template = (args) => <Button {...args} />;  export const Red = Template.bind({});  Red.args = { 	backgroundColor: "red", 	label: "Click Me", 	size: "md", 	color: "white", };  export const Green = Template.bind({}); Green.args = { 	backgroundColor: "rebeccapurple", 	label: "Click Me", 	size: "md", 	color: "white", };  export const Small = Template.bind({}); Small.args = { 	backgroundColor: "yellow", 	label: "Click Me", 	size: "sm", };  export const Large = Template.bind({}); Large.args = { 	backgroundColor: "green", 	label: "Click Me", 	size: "lg", };  export const LongLabel = Template.bind({}); LongLabel.args = { 	backgroundColor: "pink", 	label: "Press Me adsf asd", 	size: "md", }; 

Steps to Run the Application:

Step 1: Use the following command to start your React application:

npm start

Step 2: Go to Storybook URL in your web browser:

 http://localhost:6006

Output:

Explanation: The "Button.stories.js" file configures Storybook for the "Button" component in react which is used to define the variations with different colors, sizes, and labels. You can create every variation using a template function, which allows you to build concise and reusable code. A functional React component that can dynamically style buttons according to their size and colour is defined in the "Button.js" file. Validation of the type is conducted using PropTypes in order to ensure proper use of prop. The efficient development, testing and documentation of the React 'Button' element in Storybook environment can be facilitated through this structure approach.


Next Article
Survey Website using ReactJS

D

dikshashu9v51
Improve
Article Tags :
  • Project
  • Web Technologies
  • ReactJS
  • Geeks Premier League
  • ReactJS-Projects
  • Geeks Premier League 2023

Similar Reads

  • Typing Game using React
    React Typing Game is a fun and interactive web application built using React. It tests and improves the user's typing speed and accuracy by presenting sentences for the user to type within a specified time limit. The game provides real-time feedback by highlighting any mistakes made by the user, mak
    4 min read
  • React MUI Routing
    React MUI is a UI library that provides fully-loaded components, bringing our own design system to our production-ready components. MUI is a user interface library that provides predefined and customizable React components for faster and easy web development, these Material-UI components are based o
    3 min read
  • Survey Website using ReactJS
    In this article, we will create a survey website template using ReactJS. This project basically creates a single page website which will take input from user in multiple states and stores it to be displayed later. The user has to enter basic details like name, email-Id and contact number. After fill
    10 min read
  • Create ToDo App using ReactJS
    In this article, we will create a to-do app to understand the basics of ReactJS. We will be working with class based components in this application and use the React-Bootstrap module to style the components. This to-do list can add new tasks we can also delete the tasks by clicking on them. The logi
    3 min read
  • ReactJS | Using Babel
    Now we know that what Babel is, we will focus on how to install it on your machine using node. Babel can be installed easily after following these simple steps. Requirements : A code editor like atom, sublime text or Visual studio code.Node should be installed on the machine with npm too. We will in
    2 min read
  • React Rebass Theming
    React Rebass is a front-end framework that was designed keeping react in mind. In this article, we will know what are Props in React Rebass. Props are an important component that is required in each development. Theming is used to provide colors, typographic styles, layout styles, and component vari
    3 min read
  • React Tutorial
    React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable. Applications are built using reusable compo
    8 min read
  • Math Sprint Game using React
    In this article, we will create a Math Sprint Game Using ReactJS. Math Sprint is a fun and challenging game where players have to solve math problems within a time limit to earn points. This game presents random math questions with four options. Players click the correct answer, and if correct, it’s
    5 min read
  • Blog app using ReactJS
    In this article, we have created the blog app using react js, First of all, we have created the project name blog by entering the command npx create-react-app blog and installing all modules. Then we create the folder name component under src and make two jsx file post.jsx and posts.jsx and styling
    5 min read
  • Shopping Cart app using React
    In this article, we will create an Interactive and Responsive Shopping Cart Project Application using the famous JavaScript FrontEnd library ReactJS. We have named or Application as "GeeksforGeeks Shopping Cart". The application or project which we have developed mainly focuses on the functional com
    9 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences