storybook: [Bug]: array or object with JSX elements in context causes Storybook to hang indefinitely
Describe the bug
Providing an array of JSX elements, or an object containing JSX elements, as the value in a context provider causes Storybook to hang indefinitely. The UI may load but it cannot be interacted with once you click on the story with the context issue.
This only happens with arrays/objects. Passing a JSX element as the value in context works fine.
Example code of adding context in a story (with an array):
const Template = (args) => (
<FooContext.Provider value={[<div>Hi</div>]}>
<FooComponent {...args} />
</FooContext.Provider>
);
This also happens if the context is placed in decorators, e.g. (with an object):
export default {
...csf,
decorators: [
(Story) => (
<FooContext.Provider value={{ test: <div>Hi</div> }}>
<FooComponent />
</FooContext.Provider>
)
]
}
One solution is to use the storybook-react-context add-on, however if there is something I have missed that is causing this issue I would welcome guidance on solving it without using an add-on.
To Reproduce
https://github.com/alicnik/storybook-jsx-array-context-bug
System
System:
OS: macOS 12.6
CPU: (8) x64 Intel(R) Core(TM) i7-8559U CPU @ 2.70GHz
Binaries:
Node: 16.18.0 - /usr/local/bin/node
Yarn: 3.2.4 - /usr/local/bin/yarn
npm: 8.19.2 - /usr/local/bin/npm
Browsers:
Chrome: 106.0.5249.119
Firefox: 102.0.1
Safari: 16.0
npmPackages:
@storybook/addon-docs: ^7.0.0-alpha.40 => 7.0.0-alpha.40
@storybook/addon-essentials: ^7.0.0-alpha.40 => 7.0.0-alpha.40
@storybook/addon-interactions: ^7.0.0-alpha.40 => 7.0.0-alpha.40
@storybook/addon-links: ^7.0.0-alpha.40 => 7.0.0-alpha.40
@storybook/react: ^7.0.0-alpha.40 => 7.0.0-alpha.40
@storybook/react-webpack5: ^7.0.0-alpha.40 => 7.0.0-alpha.40
@storybook/testing-library: ^0.0.13 => 0.0.13
Additional context
No response
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 11
- Comments: 20 (8 by maintainers)
Commits related to this issue
- Attempt repro for #19575 — committed to storybookjs/storybook by shilman 3 years ago
- Attempt repro for #19575 — committed to storybookjs/storybook by shilman 3 years ago
- Fix the version of `react-element-to-jsx-string` See https://github.com/storybookjs/storybook/issues/19575 for explanation — committed to storybookjs/storybook by tmeasday 2 years ago
After some digging, it seems to be the same issue at heart as https://github.com/storybookjs/storybook/issues/12747. @burdisal perhaps the workaround there will work for you? Adding this to
parametersin the story solved the issue for me:@alicnik thanks, added to global parameters in preview.js and it worked
Can folks who are seeing this try pinning the version of
react-element-to-jsx-stringto see if it indeed does help? Inpackage.json:npm:
yarn:
The explanation for this issue is that
react-element-to-jsx-stringdoesn’t like it when you pass props that contain rendered elements.See https://github.com/algolia/react-element-to-jsx-string/issues/681
We already filter out pure elements and arrays that contain elements (https://github.com/storybookjs/storybook/issues/17482). It seems we still have an issue with objects that contain elements.
It sounds like maybe pinning the version of
react-element-to-jsx-stringto14.3.2may fix the issue. Why don’t we just do that if it is unmaintained @shilman @IanVS ?@tmeasday Thanks, that seems to have fixed the issue.