storybook: Component in object prop freezes the story

Describe the bug Component in object prop freezes the story. If I provide an object prop like this:

bottomLine={{
  icon: <IconAgenda />,
  text: args.bottomLineText,
}}

The component is not rendered or renders really slow. Also the actions don’t work. The following component renders as expected:

// works
<Button
  topLine={args.topLineText}
/>

// does not render correctly (action add-on doesn't work)
<Button
  topLine={args.topLineText}
  bottomLine={{
    icon: <Component />,
    text: args.bottomLineText,
  }}
/>

To Reproduce Clone the repro -> npm install -> npm run storybook -> change text in action of DoesNotWorkButton story

System

Environment Info:

  System:
    OS: macOS 11.6.4
    CPU: (8) x64 Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
  Binaries:
    Node: 16.2.0 - ~/.nvm/versions/node/v16.2.0/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 7.13.0 - ~/.nvm/versions/node/v16.2.0/bin/npm
  Browsers:
    Chrome: 99.0.4844.51
    Firefox: 98.0.1
    Safari: 14.1.2
  npmPackages:
    @storybook/addon-actions: ^6.4.19 => 6.4.19 
    @storybook/addon-essentials: ^6.4.19 => 6.4.19 
    @storybook/addon-interactions: ^6.4.19 => 6.4.19 
    @storybook/addon-links: ^6.4.19 => 6.4.19 
    @storybook/addon-toolbars: ^6.4.19 => 6.4.19 
    @storybook/builder-webpack5: ^6.4.19 => 6.4.19 
    @storybook/manager-webpack5: ^6.4.19 => 6.4.19 
    @storybook/node-logger: ^6.4.19 => 6.4.19 
    @storybook/preset-create-react-app: ^4.0.1 => 4.0.1 
    @storybook/react: ^6.4.19 => 6.4.19 
    @storybook/testing-library: ^0.0.9 => 0.0.9 

Additional context This pattern used to work in previous versions of Storybook. When '@storybook/addon-docs' is not used, everything works as expected.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 24
  • Comments: 15 (3 by maintainers)

Most upvoted comments

I noticed that the JSX rendering code sometimes gets stuck in an infinite recursion loop.

If you add this to preview.jsx does the problem go away?

export const parameters = {
  docs: {
    source: {
      // any non-empty string here will skip jsx rendering, see:
      // https://github.com/storybookjs/storybook/blob/next/code/renderers/react/src/docs/jsxDecorator.tsx#L165
      code: "hello world",
    },
  },
};

If you’re using 7.0 you can disable per story like so:

export const MyStory: StoryObj<typeof Component> = {
  render: (args) => {
    return (
      <>
        <Component
          {...args}
          slots={{
            prefix: <SearchIcon />
          }}
        />
      </>
    );
  },
  parameters: {
    docs: {
      source: {
        code: "disabled"
      }
    }
  }
};

For me, rendering the component outside the story helped:

Hangs:

export const Icons: Story<ButtonProps> = args => {
  return (
    <PropsTable
      component={Button}
      commonProps={args}
      columnTitle={xProps => Object.keys(xProps)[0]}
      xProps={[{ startIcon: <IconArrowRightArrowLeftHorizontalSMIcon /> }, { endIcon: <IconArrowRightArrowLeftHorizontalSMIcon /> }]}
      yProps={[
        { size: "x-large" },
        { size: "large" },
        { size: "medium" },
        { size: "small" },
      ]}
    />
  );
};

Works:

const icon = <IconArrowRightArrowLeftHorizontalSMIcon />;
export const Icons: Story<ButtonProps> = args => {
  return (
    <PropsTable
      component={Button}
      commonProps={args}
      columnTitle={xProps => Object.keys(xProps)[0]}
      xProps={[{ startIcon: icon }, { endIcon: icon }]}
      yProps={[
        { size: "x-large" },
        { size: "large" },
        { size: "medium" },
        { size: "small" },
      ]}
    />
  );
};

@matthew-dean removing the story args like this fixed the issue

export const Basic: Story<StoryProps> = () => (
    <OurComponent args={args} />
)

But this is really undesirable behaviour 😢

Note, we’re also seeing huge story freezes. Up to 40 seconds for a single story.

For us, it wasn’t related to component args. Changing from this:

// Simplified
const Template: Story = (args) => (
  <OurComponent args={args} />
)
export const Basic: Story<StoryProps> = Template.bind({})
Basic.args = args

to this:

export const Basic: Story<StoryProps> = () => (
    <OurComponent args={args} />
)

…dropped the delay / render time from 40 seconds to zero!!

I encountered the same problem with having array of react elements in props and was trying to debug it. I didn’t find much but I saw that the infinite (or just extremely long running) loop begins from renderJsx function from jsxDecorator.tsx calling the reactElementToJSXString.

I have a hunch that the JSX which is being consumed by this function, is already rendered in the storybook, so it already has the _owner field set to a Fiber node. And then the recursive logic that serializes the props, starts consuming the Fiber node which is a cyclic object, falling into infinite/expensive loop.

Proably it’s the react-element-to-jsx-string package that could be blamed for that but I believe it would be better if Storybook could mitigate the issue instead on its side (probably by somehow transforming the props passed to reactElementToJSXString? Cloning them but with fiber nodes removed? cloning them but passing react elements through React.cloneElement?)

– EDIT I just realized this issue is dead and it is being continued here: https://github.com/storybookjs/storybook/issues/19575