Skip to content

Instantly share code, notes, and snippets.

@andyrichardson
Last active November 17, 2020 11:30
Show Gist options
  • Save andyrichardson/4e0c2ab5ef15cb1a7ce8602dbec62f88 to your computer and use it in GitHub Desktop.
Save andyrichardson/4e0c2ab5ef15cb1a7ce8602dbec62f88 to your computer and use it in GitHub Desktop.
Example decorator for using

.storybook/preview.tsx

import { addDecorator, DecoratorFn } from '@storybook/react';

/** Adds react-router context */
const routerDecorator: DecoratorFn = (Story, context) => {
  const router = context.parameters.router;

  return (
    <MemoryRouter initialEntries={router?.initialEntries || ['/']} initialIndex={router?.initialIndex ?? 0}>
      <Story {...context} />
    </MemoryRouter>
  );
};

addDecorator(routerDecorator)

storybook.d.ts

import "@storybook/addons";

declare module '@storybook/addons' {
  export interface Parameters {
    router?: {
      /* Initial history state */
      initialEntries?: string[];
      /* Initial position in state */
      initialIndex?: number;
    };
  }
}

Component.fixture.tsx

export const MyStory: Story = () => <Component />
Story.parameters = {
  router: {
    initialEntries: ['/']
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment