storybook: [app: react] HMR causes story components to unmount way too aggressively

Describe the bug

In a plain, completely unmodified create-react-app project, I can make edits with hot module replacement and my components don’t unmount most of the time.

In a Storybook, if I sneeze, my components unmount.

Okay, I’m joking about the sneeze, but seriously: I can have a story from file A open, and edit file B, with no dependency relationship between the two files at all (except for the fact that they are both automatically depended on by the preview entry point), and everything in my current story will unmount. Most of the time, the re-rendering is fast enough that this isn’t noticeable, but when mounting and unmounting matters, it really matters.

To Reproduce

Steps to reproduce the behavior:

  1. Clone https://github.com/rhendric/super-duper-computing-machine
  2. Run yarn storybook
  3. Select the MountCounter story
  4. Edit src/stories/Other.stories.js
  5. Watch the unmount count tick upwards
  6. Edit the prop field in src/stories/MountCounter.stories.js.
  7. Watch the unmount count keep going
  8. For comparison, run yarn start and make edits to src/index.js, and watch the unmount count not budge.

Expected behavior

Hot module replacement should not force a remount of the current story, unless the current story no longer exists or something catastrophic like that. It’s fine if React itself unmounts a component because its code was replaced, but I think the only time Storybook should force a remount is if Storybook changed the currently active story.

System:

  System:
    OS: Linux 5.6 Arch Linux
    CPU: (2) x64 Intel(R) Core(TM)2 Duo CPU     P8600  @ 2.40GHz
  Binaries:
    Node: 14.0.0 - /usr/bin/node
    Yarn: 1.22.4 - ~/.local/bin/yarn
    npm: 6.14.4 - /usr/bin/npm
  npmPackages:
    @storybook/preset-create-react-app: ^2.1.1 => 2.1.1 
    @storybook/react: ^5.3.18 => 5.3.18 

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 27 (16 by maintainers)

Most upvoted comments

OK I created a ticket: https://github.com/storybookjs/storybook/issues/11212

I’ve added it to the 6.0 milestone. Not sure when I’ll get to it but hopefully before then!

Hey, that sounds like good progress, but will it address the case of HMRing the current story? From my last look at the SB5 code (from several weeks ago; my memory may be rusty), calling setStory passes down a boolean that ultimately causes SB to force an unmount of the story before rendering, which makes sense if the story is actually changing, but if the story is just a new revision of the same story, I don’t think it should be force-unmounted (React should be smart enough to unmount things if it needs to). From your description, it doesn’t sound like that’s going to change; is that correct, and if so why?

I’d like to look at this as part of 6.0 qa