Storybook is an open-source tool for developing UI components in isolation for React, Vue, Angular, and more. It enables developers to build, test, and showcase their components independently, making
Blog
-
Imagine you're part of a team developing a web application for an e-commerce platform. Your task is to create a product card component that displays essential information about each item for sale. Here's how Storybook can be instrumental in this scenario:
lets suppose you have button component :
// Button.js
import React from 'react';
const Button = ({ onClick, children }) => {
return (
<button onClick={onClick} style={{ padding: '10px', backgroundColor: 'blue', color: 'white', border: 'none', borderRadius: '5px', cursor: 'pointer' }}>
{children}
</button>
);
}
export default Button;
Now, let's create a Storybook for this Button component:
npx sb init
2. Create a file name 'Button.stories.js' in your src directory:
// Button.stories.js
import React from 'react';
import { storiesOf } from '@storybook/react';
import Button from './Button';
// Create a "storiesOf" for Button component
storiesOf('Button', module)
.add('with text', () => <Button onClick={() => console.log('Button clicked')}>Click me</Button>)
.add('with emoji', () => <Button onClick={() => console.log('Button clicked')}>๐ ๐ ๐ ๐ฏ</Button>);
3. Run storybook :
npm run storybook
4. Now, you can see your Button component stories in the Storybook UI. You can interact with them and view different variations of your Button component.
This is a basic example, but Storybook allows for much more complex setups and configurations. You can add addons, customize the UI, and even mock different states and props for your components to visualize them in various scenarios.
Copyright ยฉ2024 Preplaced.in
Preplaced Education Private Limited
Ibblur Village, Bangalore - 560103
GSTIN- 29AAKCP9555E1ZV