storybook: Storybook crashes in browser when array wrapper syntax is used inside an arrow-function component with arguments
Describe the bug
When running storybook using start-storybook, rendering a component using arrow function syntax with one or more arguments, then adding a prop inside that component that renders elements in an array, causes Storybook to crash (see example below). After running storybook and navigating to this story, storybook will completely freeze and the tab will need to be closed.
This crash doesn’t occur with a static build (running build-storybook and then opening the result in a browser).
To Reproduce
The following story will crash:
<!-- Example.stories.mdx -->
import { Canvas, Meta, Story } from '@storybook/addon-docs';
export const Example = () => 'Example';
<Meta title="Components/Example" component={Example} />
<Canvas>
<Story name="Example">{(args) => <Example array={[<div key="foo">foo</div>, <div key="bar">bar</div>]} />}</Story>
</Canvas>
Step-by-step reproduction:
- Add the aforementioned story to your storybook
- Run storybook using
start-storybook(this bug doesn’t happen when using a static build) - Navigate to the story in your browser (note that doing so will make your browser tab crash)
The arrow function declared as a child of our <Story> element is the culprit. The crash appears to go away if you do either one of the following:
- Remove any arguments from the arrow function
- Remove the array syntax from within the function body (delete it, replace it with fragment syntax, or factor it out so that it’s no longer declared inside the arrow function)
System
Environment Info:
System:
OS: macOS 12.0.1
CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
Binaries:
Node: 14.17.3 - ~/.nvm/versions/node/v14.17.3/bin/node
npm: 6.14.15 - ~/.nvm/versions/node/v14.17.3/bin/npm
Browsers:
Chrome: 96.0.4664.110
Edge: 95.0.1020.53
Firefox: 89.0.2
Safari: 15.1
npmPackages:
@storybook/addon-a11y: ^6.4.9 => 6.4.9
@storybook/addon-controls: ^6.4.9 => 6.4.9
@storybook/addon-docs: ^6.4.9 => 6.4.9
@storybook/addon-links: ^6.4.9 => 6.4.9
@storybook/addon-toolbars: ^6.4.9 => 6.4.9
@storybook/addon-viewport: ^6.4.9 => 6.4.9
@storybook/addons: ^6.4.9 => 6.4.9
@storybook/builder-webpack5: ^6.4.9 => 6.4.9
@storybook/manager-webpack5: ^6.4.9 => 6.4.9
@storybook/react: ^6.4.9 => 6.4.9
@storybook/theming: ^6.4.9 => 6.4.9
Additional context Occurs in the following browsers:
Google Chrome Version 96.0.4664.93 Firefox 89.0.2 Safari Version 15.1
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 16
- Comments: 17 (1 by maintainers)
For us, the breaking part in the
addon-docswas the Dynamic Source Code rendering for React. So I added the following in the Storybookpreview.jsand that fixed the issue:Of course, the drawback here is that the Source code showing up for every story in the Docs tab will be the source code of the story itself instead of being a JSX version of it, but for us it is definitely better to have that than having to disable the
addon-docsplugin entirely.Still experiencing this issue with
7.0.6Yes, having the same issue here. The weird thing is that I uninstalled
@storybook/addon-docsand removed every single line that mentioned it in my project, and it was still being loaded in storybook! I had to manuallyrm -rf node_modules/@storybook/addon-docsto really make it go.And yet…it is still freezing the browser when I have a story like this:
Interestingly enough (or not), if you simply move it to somewhere els (out of the story function) it works as expected.
Of course, in this case, you simply can’t use the controls to affect those elements.
One more useful info.
If you use
maporspliceinelsListit will still work. That makes me believe it’s exclusively related to rendering child elements in the story render, not dealing with child elements in it.It’s fixed my issue. Thanks!
I think I encountered the same error. It happens in other story formats too.
Same with
7.1.0I’m not sure my crashes are related but, removing
@storybook/addon-docseliminated them. I was seeing deep call stacks in the react-element-to-jsx-string module. See https://github.com/algolia/react-element-to-jsx-string/issues/681@Floffah Great idea, thank you! Sadly, this won’t help when we need dynamic props in these JSX chunks, but at least it’s possible to deal with static props.
I get the same problem with this code:
as soon as i remove the items array it unfreezes
a workaround i found that fixes the problem is by changing the array function to pass the items from a separate variable: