Back to integrations
Add your integration
Categories
  • ⭐️ Popular
  • 🧩 Essentials
  • 🛠 Code
  • ⚡️ Data & state
  • ✅ Test
  • 💅 Style
  • 🎨 Design
  • ⚙️ Appearance
  • 🗄 Organize
How to install addons Create an addon
storybook-fetch-addon
Fetch data from api
npm install storybook-fetch-addon
Last updated about 5 years ago
26
Downloads per week
Readme View on GitHub

storybook-fetch-addon

Storybook addon for fetching data from api

Install

npm i --save-dev storybook-fetch-addon

Setup

// In addons.js

import 'storybook-fetch-addon/register';
// In config.js

import { addDecorator, addParameters } from '@storybook/react';
import { withFetch } from 'storybook-fetch-addon';

addDecorator(withFetch);

addParameters({
  fetch: {
     // Required:
    fetch: param => fetch(`https://my-api.com?id=${param}`),

    // Optional:
    map: data => { 
      // transform data from api
      return props;
    },
    valid: value => { 
      // Validate value
      return valid;
    },
    defaultProps: {
      return { /* default props */ };
    }
  }

API

fetchSingle

(id, component, defaultValue) => Component

  • id: string that identifies the item.
  • component: React component that will be passed the fetched props.
  • defaultValue: optional string that will be used as default value for fetching data.

fetchList

(items, component) => Component

  • items: array of id/defaultValue pairs. Default value is optional.
  • component: React component that will be passed the fetched props for all items.

Example

import { fetchSingle, fetchList } from 'storybook-fetch-addon';

storiesOf('My Component')
  .add('single', () => fetchSingle(
    'Item 1', 
    ({ props, loaded, error }) => <MyComponent {...props} />,
    'default value'
  ))
  .add('list', () => fetchList(
    [
      ['Item 1', 'default 1'],
      ['Item 2', 'default 2']
    ],
    ({ items }) => (
      <ul>
        {items.map(({ props, loaded, error }) => <MyComponent key={props.id} {...props} />)}
      </ul>
    )
  ));
Join the community
6,595 developers and counting
WhyWhy StorybookComponent-driven UI
DocsGuidesTutorialsChangelogTelemetryStatus
CommunityAddonsGet involvedBlog
ShowcaseExploreProjectsComponent glossary
Open source software
Storybook

Maintained by
Chromatic
Special thanks to Netlify and CircleCI