Decorator unmounted when switching stories
See original GitHub issueAdditional Context
I’m trying to create a storybook addon for my company. We’re looking to create a decorator which will hold a stateful object (our company’s sdk) via the react’s context api. After that we’ll have stories which will make use of this context to load some data.
This addon is part of our web solution, and we work in a yarn workspaces environment with boilerplates made with tsdx. Don’t worry I’ve provided a clonable repro repo 😃
Describe the bug
The decorator works fine when clicking on the toolbar’s button, we can see the color changing from green to red. The problem is that the decorator gets unmounted/remounted when you visit another story. I’ve added console.log
statement to help debug.
To Reproduce
git clone https://github.com/y-nk/storybook-decorator-unmounted-repro
cd storybook-decorator-unmounted-repro
yarn
cd addon
yarn build
cd ../demo-app
yarn storybook
(oryarn build-storybook
andnpx http-server storybook-static
)
or
System
Environment Info:
System:
OS: macOS 11.4
CPU: (8) x64 Intel(R) Core(TM) i5-8257U CPU @ 1.40GHz
Binaries:
Node: 16.4.0 - ~/.nvm/versions/node/v16.4.0/bin/node
Yarn: 1.22.4 - /usr/local/bin/yarn
npm: 7.18.1 - ~/.nvm/versions/node/v16.4.0/bin/npm
Browsers:
Chrome: 91.0.4472.114
Firefox: 88.0.1
Safari: 14.1.1
EDIT: I forgot to mention but you can tinker around by running yarn watch
in the addon side, and yarn storybook
in the demo-app side.
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (7 by maintainers)
@y-nk I guess so, but I do wonder if that would be confusing in other situations…
Without thinking too deeply about it yet, the problem there is the set of decorators differs between different stories, so the
DecoratorStack
“component” will be different as you change stories, and so React will see<DecoratorStack>
as a new component that it needs to remount (along with its rendered subcomponents) each time you change story.It would be nice to be able to produce something like:
But the issue is that the decorator API allows
DecoratorOne
to pass updated context toDecoratorTwo
, so it need to take it unrendered as an prop, rather than as a child.