#A StoryWithHooks and Typescript

3 messages · Page 1 of 1 (latest)

simple forge
#

Hi, the example found here https://storybook.js.org/docs/react/writing-stories/introduction#working-with-react-hooks doesn't show any types like the other examples and I am still in the process of picking up TypeScript so I was hoping someone could confirm or suggest the correct way to type the following:

export default {
  title: 'Components/Atoms/Map',
  component: MapComponent,
  args: props,
} as Meta<typeof MapComponent>;

const MapWithHooks: StoryObj<typeof MapComponent> = (args) => {
  const dispatch = useAppDispatch();

  dispatch(
    setOrganisation({
      ...
    })
  );

  return <MapComponent {...args} />;
};

export const Map = {
  render: (args) => <MapWithHooks {...args} />,
};

Storybook is a frontend workshop for building UI components and pages in isolation. Thousands of teams use it for UI development, testing, and documentation. It’s open source and free.

viral knoll
#

Hi! In your example above, MapWithHooks is a React component. Map is the story. So, you could type it like so:

export default {
  title: 'Components/Atoms/Map',
  component: MapComponent,
  args: props,
} as Meta<typeof MapComponent>;

const MapWithHooks = (args: React.ComponentProps<typeof MapComponent>) => {
  const dispatch = useAppDispatch();

  dispatch(
    setOrganisation({
      ...
    })
  );

  return <MapComponent {...args} />;
};

export const Map: StoryObj<typeof MapComponent> = {
  render: (args) => <MapWithHooks {...args} />,
};
viral knoll