Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

storybook-addon-react-router-v6

JesusTheHun324.1kUnlicense2.0.15TypeScript support: included

Use React Router v6 in your stories

storybook-addons, react-router, react-router-v6, router

readme

Storybook Addon React Router v6

Storybook npm Release npm

Use React Router v6 in your stories.

This is the documentation for version 2.x. If you are still using version 1.x, visit v1 documentation.

Recent changes

✅You can now use useStoryElement to inject the story at multiple points. ✅The routing parameter now accept a string, that will be used both as the route path and the location pathname.

Getting Started

Install the package

yarn add -D storybook-addon-react-router-v6

Add it to your storybook configuration:

// .storybook/main.ts

export default {
  addons: ['storybook-addon-react-router-v6'],
} satisfies StorybookConfig;

Decorate all stories of a component

To add the router to all the stories of a component, simply add it to the decorators array.

Note that parameters.reactRouter is optional, by default the router will render the component at /.

import { withRouter } from 'storybook-addon-react-router-v6';

export default {
  title: 'User Profile',
  render: () => <UserProfile />,
  decorators: [withRouter],
  parameters: {
    reactRouter: reactRouterParameters({
      location: {
        pathParams: { userId: '42' },
      },
      routing: { path: '/users/:userId' },
    }),
  },
};

Decorate a specific story

To change the config for a single story, you can do the following :

import { withRouter } from 'storybook-addon-react-router-v6';

export default {
  title: 'User Profile',
  render: () => <UserProfile />,
  decorators: [withRouter],
};

export const FromHomePage = {
  parameters: {
    reactRouter: reactRouterParameters({
      location: {
        pathParams: { userId: '42' },
        searchParams: { tab: 'activityLog' },
        state: { fromPage: 'homePage' },
      },
      routing: {
        path: '/users/:userId',
        handle: 'Profile',
      },
    }),
  },
};

Decorate all stories, globally

To wrap all your project's stories inside a router by adding the decorator in your preview.js file.

// .storybook/preview.js

export default {
  decorators: [withRouter],
  parameters: {
    reactRouter: reactRouterParameters({ ... }),
  }
} satisfies Preview;

Location

To specify anything related to the browser location, use the location property.

type LocationParameters = {
  path?: string | ((inferredPath: string, pathParams: Record<string, string>) => string | undefined);
  pathParams?: PathParams;
  searchParams?: ConstructorParameters<typeof URLSearchParams>[0];
  hash?: string;
  state?: unknown;
};

Inferred path

If location.path is not provided, the browser pathname will be generated using the joined paths from the routing property and the pathParams.

Path as a function

You can provide a function to path.
It will receive the joined paths from the routing property and the pathParams as parameters.
If the function returns a string, is will be used as is. It's up to you to call generatePath from react-router if you need to.
If the function returns undefined, it will fallback to the default behavior, just like if you didn't provide any value for location.path.

Routing

You can set routing to anything accepted by createBrowserRouter.
To make your life easier, storybook-addon-react-router-v6 comes with some routing helpers :

export const MyStory = {
  parameters: {
    reactRouter: reactRouterParameters({
      routing: reactRouterOutlet(<MyOutlet />),
    }),
  },
};

Routing Helpers

The following helpers are available out of the box :

reactRouterOutlet(); // Render a single outlet
reactRouterOutlets(); // Render multiple outlets
reactRouterNestedOutlets(); // Render multiple outlets nested one into another
reactRouterNestedAncestors(); // Render the story as an outlet of nested outlets

You can also create your own helper and use the exported type RoutingHelper to assist you :

import { RoutingHelper } from 'storybook-addon-react-router-v6';

const myCustomHelper: RoutingHelper = () => {
  // Routing creation logic
};

RouterRoute is basically the native RouteObject from react-router; augmented with { useStoryElement?: boolean }. If you want to accept a JSX and turn it into a RouterRoute, you can use the exported function castRouterRoute.

Use the story as the route element

Just set { useStoryElement: true } in the routing config object.

Dedicated panel

Navigation events, loader and actions are logged, for you to better understand the lifecycle of your components.

Addon Panel

Compatibility

Version 6.4+ of react-router is required. This package aims to support Storybook > 7 and React > 16.

✅ Storybook 7.0

✅ React 16
✅ React 17
✅ React 18

If you have an issue with any version, open an issue.

Contribution

Contributions are welcome.

Before writing any code, file an issue to showcase the bug or the use case for the feature you want to see in this addon.

changelog

v2.0.11 (Sun Mar 03 2024)

⚠️ Pushed to main

Authors: 1


v2.0.10 (Tue Nov 14 2023)

⚠️ Pushed to main

  • fix(build): change module target to include import assertions into result (@JesusTheHun)

Authors: 1


v2.0.9 (Fri Nov 03 2023)

🐛 Bug Fix

Authors: 1


v2.0.8 (Tue Oct 17 2023)

⚠️ Pushed to main

  • fix(panel): import router context from react-router-dom (@JesusTheHun)

Authors: 1


v2.0.7 (Tue Aug 29 2023)

⚠️ Pushed to main

  • fix(logs): loader and action are now logged correctly if the functions come from the lazy property. (@JesusTheHun)

Authors: 1


v2.0.6 (Tue Aug 29 2023)

⚠️ Pushed to main

  • fix(logs): current url now correctly add question mark and sharp character. (@JesusTheHun)
  • fix(logs): missing question mark before query string (@JesusTheHun)
  • build(deps): remove react-router from the peerDependencies (@JesusTheHun)

Authors: 1


v2.0.5 (Fri Aug 18 2023)

⚠️ Pushed to main

  • fix(decorator): story not updating on arg change if the story itself is an outlet (@JesusTheHun)
  • test(controls): bring back addon-essentials (@JesusTheHun)

Authors: 1


v2.0.4 (Sun Aug 13 2023)

⚠️ Pushed to main

Authors: 1


v2.0.3 (Fri Aug 11 2023)

⚠️ Pushed to main

  • fix(panel): duplicate event key when hmr triggers (@JesusTheHun)

Authors: 1


v2.0.2 (Fri Aug 04 2023)

⚠️ Pushed to main

  • test(event): the suffix of the first event is now 1 (@JesusTheHun)
  • fix(event): duplicate event key when hmr triggers before any new event (@JesusTheHun)
  • fix(panel): use template literal for styled components (@JesusTheHun)

Authors: 1


v2.0.1 (Fri Aug 04 2023)

⚠️ Pushed to main

  • feat(panel): add banner when a new version is available (@JesusTheHun)
  • fix(panel): update panel registration title to return a ReactElement (@JesusTheHun)
  • fix(npm): add required package at build time to dev deps (@JesusTheHun)
  • fix(helper): remove forgotten log statement (@JesusTheHun)

Authors: 1


v1.0.2 (Thu Jun 01 2023)

⚠️ Pushed to main

Authors: 1


v1.0.1 (Tue May 23 2023)

⚠️ Pushed to main

Authors: 1


v0.3.6 (Wed May 03 2023)

⚠️ Pushed to main

  • fix(router): rebuild router only after story args changed (@JesusTheHun)
  • feat(decorator): add support for shouldRevalidate function (@JesusTheHun)
  • fix(controls): router not reloading component after change through SB controls (@JesusTheHun)

Authors: 1


v0.3.5 (Fri Mar 24 2023)

⚠️ Pushed to main

  • fix: reloading component should not trigger a console warning (@JesusTheHun)

Authors: 1


v0.3.4 (Mon Mar 20 2023)

⚠️ Pushed to main

Authors: 1


v0.3.3 (Mon Mar 20 2023)

⚠️ Pushed to main

  • fix: story decorator not passing errorElement to the wrapping component (@JesusTheHun)
  • fix: story decorator not passing routeHandle to the wraping component (@JesusTheHun)

Authors: 1


v0.3.2 (Sun Mar 19 2023)

⚠️ Pushed to main

Authors: 1


v0.3.1 (Wed Mar 15 2023)

⚠️ Pushed to main

Authors: 1


v0.2.2 (Mon Mar 13 2023)

⚠️ Pushed to main

Authors: 1


v0.1.15 (Tue Aug 30 2022)

⚠️ Pushed to main

Authors: 1


v0.1.14 (Fri Aug 19 2022)

⚠️ Pushed to main

Authors: 1


v0.1.13 (Tue Aug 02 2022)

⚠️ Pushed to main

  • fix: query string question mark inconsistencies (@JesusTheHun)

Authors: 1


v0.1.12 (Tue Aug 02 2022)

⚠️ Pushed to main

  • test: add basic nested routing stories (@JesusTheHun)
  • change(event): remove matchedRoutes prop as the fix cannot be delivered before the next react-router version (@JesusTheHun)
  • fix: double parsing of route pattern (@JesusTheHun)
  • readme: add tiny contribution guideline (@JesusTheHun)

Authors: 1


v0.1.11 (Fri Jul 29 2022)

⚠️ Pushed to main

Authors: 1


v0.1.10 (Thu Jun 23 2022)

⚠️ Pushed to main

Authors: 1


v0.1.9 (Wed Jun 22 2022)

⚠️ Pushed to main

Authors: 1


v0.1.8 (Mon Jun 06 2022)

⚠️ Pushed to main

Authors: 1


v0.1.7 (Wed May 11 2022)

⚠️ Pushed to main

  • fix: double initial rendering navigation event (@JesusTheHun)
  • misc: better event name (@JesusTheHun)
  • feat: initial rendering no longer increment the counter in the panel title (@JesusTheHun)
  • feat: use header component to test router decorator (@JesusTheHun)

Authors: 1


v0.1.6 (Wed May 11 2022)

⚠️ Pushed to main

Authors: 1


v0.1.5 (Wed May 11 2022)

⚠️ Pushed to main

Authors: 1


v0.1.4 (Wed May 11 2022)

⚠️ Pushed to main

Authors: 1


v0.1.1 (Wed May 11 2022)

⚠️ Pushed to main

Authors: 1