Search code examples
reactjstypescriptstorybook

call React Hooks in global decorator for Storybook


I want to call custom react hook for all stories, however, im not sure where to declare. The custom hooks I wanna call must be in context provider.

Are there any ways to declare context provider and custom hooks for all stories?

import React from 'react';
import { useCustomHooks } from './custom';
import { ThemeProvider } from 'styled-components';

export const decorators = [
  (Story) => (
    useCustomHooks() // I wanna call custom hooks here but not working because not in provider
    <ThemeProvider theme="default">
      <Story />
    </ThemeProvider>
  ),
];


Solution

  • Yes, in .storybook/preview.js:

    import { addDecorator } from '@storybook/react';
    
    addDecorator((Story) =>
      <MotionLazyContainer>
        <ThemeProvider>
          <Story />
        </ThemeProvider>
      </MotionLazyContainer>
    );