Loading the good stuff
storybook.js.orgstorybook.js.org
Gradient background

Use React hooks inside an MDX story in Storybook

Clarice Bouwer

Software Engineering Team Lead and Director of Cloudsure

Thursday, 13 January 2022 · Estimated 1 minute read

Originally posted on dev.to

A story captures the rendered state of a UI component. Developers write multiple stories per component that describe all the “interesting” states a component can support.

MDX is a standard file format that combines Markdown with JSX. It means you can use Markdown’s terse syntax (such as # heading) for your documentation, write stories that compile to our component story format, and freely embed JSX component blocks at any point in the file. All at once.

A React Hook lets you use state and other React features without writing a class.

Tie the three together:

Copy
<Story name="With hooks">
  {() => {
    const [count, setCount] = useState(0);
    return (
      <button onClick={() => setCount(count + 1)}>
        Clicked {count} time(s)
      </button>
    );
  }}
</Story>