question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Decorator unmounted when switching stories

See original GitHub issue

Additional 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

  1. git clone https://github.com/y-nk/storybook-decorator-unmounted-repro
  2. cd storybook-decorator-unmounted-repro
  3. yarn
  4. cd addon
  5. yarn build
  6. cd ../demo-app
  7. yarn storybook (or yarn build-storybook and npx 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:open
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
tmeasdaycommented, Jul 20, 2021

@y-nk I guess so, but I do wonder if that would be confusing in other situations…

1reaction
tmeasdaycommented, Jun 30, 2021

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:

<DecoratorOne>
  <DecoratorTwo>
     <Story />
  </DecoratorTwo>
</DecoratorOne>

But the issue is that the decorator API allows DecoratorOne to pass updated context to DecoratorTwo, so it need to take it unrendered as an prop, rather than as a child.

Read more comments on GitHub >

github_iconTop Results From Across the Web

componentDidUpdate gets fired when I switch components in ...
So I have two components in my storybook RadioButton and RadioButtonWithIcons . On initial render of both components, my componentDidMount ...
Read more >
Addon API - Storybook - JS.ORG
Storybook may unmount your addon component, so keeping local state might not work well. Also, some addons consist of multiple parts, some parts...
Read more >
Writing Addons - Storybook
We write an addon that responds to a change in story selection like so: ... The @storybook/addons package contains a makeDecorator function which...
Read more >
Mounting and Dismounting of Rolling Bearings - Schaeffler
gineer must select the right bearing type and design and match the ... the bearings may change their chemico-physical behaviour due to.
Read more >
React Design Patterns: Return Component From Hooks
So when replacing the import statement from @mui/material/Menu to ./Menu , we can see the logs for mount and unmount while all features...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found