Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Creating Your First Story | Writing and Organizing Stories
Building UI Systems in React with Storybook

bookCreating Your First Story

When working with Storybook in React, you will use the Component Story Format, or CSF, to define stories for your UI components. CSF is a standard JavaScript syntax for writing stories as ES6 modules, which means each story is simply a named export of a function that returns a rendered component. This approach makes your stories more portable, easier to maintain, and compatible with modern tooling.

CSF offers several advantages:

  • Encourages clear, modular story definitions;
  • Integrates seamlessly with JavaScript and TypeScript;
  • Supports static analysis and code completion in editors;
  • Makes stories easy to reuse and compose.

To illustrate, consider a simple Button component. Here is how you might define the component and its first story using CSF.

// Button.js
import React from "react";

export function Button({ label, onClick }) {
  return (
    <button onClick={onClick}>
      {label}
    </button>
  );
}

// Button.stories.js
import { Button } from "./Button";

export default {
  title: "Components/Button",
  component: Button,
};

export const Primary = () => <Button label="Primary" onClick={() => {}} />;

In this example, the default export provides Storybook with metadata: the title determines where the story appears in the sidebar, and the component links the stories to the Button component. The Primary export is a function that renders the Button with specific props, creating a "Primary" button story.

Note
Note

When naming and structuring stories, use clear and descriptive names for both the story file and the stories themselves. Organize stories in folders that reflect your component hierarchy. This approach helps keep your Storybook organized and scalable as your project grows.

question mark

Which of the following statements about the Component Story Format (CSF) in Storybook is correct?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 1

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

bookCreating Your First Story

Sveip for å vise menyen

When working with Storybook in React, you will use the Component Story Format, or CSF, to define stories for your UI components. CSF is a standard JavaScript syntax for writing stories as ES6 modules, which means each story is simply a named export of a function that returns a rendered component. This approach makes your stories more portable, easier to maintain, and compatible with modern tooling.

CSF offers several advantages:

  • Encourages clear, modular story definitions;
  • Integrates seamlessly with JavaScript and TypeScript;
  • Supports static analysis and code completion in editors;
  • Makes stories easy to reuse and compose.

To illustrate, consider a simple Button component. Here is how you might define the component and its first story using CSF.

// Button.js
import React from "react";

export function Button({ label, onClick }) {
  return (
    <button onClick={onClick}>
      {label}
    </button>
  );
}

// Button.stories.js
import { Button } from "./Button";

export default {
  title: "Components/Button",
  component: Button,
};

export const Primary = () => <Button label="Primary" onClick={() => {}} />;

In this example, the default export provides Storybook with metadata: the title determines where the story appears in the sidebar, and the component links the stories to the Button component. The Primary export is a function that renders the Button with specific props, creating a "Primary" button story.

Note
Note

When naming and structuring stories, use clear and descriptive names for both the story file and the stories themselves. Organize stories in folders that reflect your component hierarchy. This approach helps keep your Storybook organized and scalable as your project grows.

question mark

Which of the following statements about the Component Story Format (CSF) in Storybook is correct?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 1
some-alt