storybook: preview > decorators causes: "Rendered more hooks than during the previous render."

Describe the bug When adding a decorator to .storybook/preview.js an error occurs: “Rendered more hooks than during the previous render.”

Error
Rendered more hooks than during the previous render.
Call Stack
 useHook
  vendors~main.iframe.bundle.js:11148:13
 useMemoLike
  vendors~main.iframe.bundle.js:11175:18
 useMemo
  vendors~main.iframe.bundle.js:11187:10
 withBackground
  vendors~main.iframe.bundle.js:3858:106
 undefined
  vendors~main.iframe.bundle.js:11056:21
 undefined
  vendors~main.iframe.bundle.js:12992:12
 undefined
  vendors~main.iframe.bundle.js:12999:14
 renderWithHooks
  vendors~main.iframe.bundle.js:249128:18
 mountIndeterminateComponent
  vendors~main.iframe.bundle.js:251954:13
 beginWork
  vendors~main.iframe.bundle.js:253192:16
Screen Shot 2021-06-13 at 15 10 48

To Reproduce Repro: TBD

Add the following code to your .storybook/preview.js

export const decorators = [Story => <Story />];

System

  System:
    OS: macOS 11.4
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
  Binaries:
    Node: 14.16.0 - ~/.nvm/versions/node/v14.16.0/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 6.14.11 - ~/.nvm/versions/node/v14.16.0/bin/npm
  Browsers:
    Chrome: 91.0.4472.101
    Firefox: 87.0
    Safari: 14.1.1
  npmPackages:
    @storybook/addon-actions: ^6.2.9 => 6.2.9 
    @storybook/addon-essentials: ^6.2.9 => 6.2.9 
    @storybook/addon-links: ^6.2.9 => 6.2.9 
    @storybook/react: ^6.2.9 => 6.2.9 

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 24
  • Comments: 35 (18 by maintainers)

Commits related to this issue

Most upvoted comments

I encountered this error after enabling strict mode and fast refresh, and was able to resolve it in my decorator function by changing <Story /> to {Story()}

i.e.

// preview.tsx ❌ errors
import {DecoratorFn} from '@storybook/react';

export const decorators: DecoratorFn[] = [
  Story => {
     return (
      <ThemeProvider>
        <Story />
      </ThemeProvider>
    );
  },
];
// preview.tsx ✅ works
import {DecoratorFn} from '@storybook/react';

export const decorators: DecoratorFn[] = [
  story => {
    return (
      <ThemeProvider>
        {story()}
      </ThemeProvider>
    );
  },
];

I am getting this error on stories that have decorators (not conditional). Changing the syntax from <Story /> to {Story()} in the decorators seems to be a viable workaround for now.

Using Storybook 7.0.18 with Vite and React.

I missed one spot where it was setting prevMountedDecorators, once I fixed that one too, now it works for both repros, so I think we have a working solution. See the linked PR

We just ran into this again, only this time it was caused by running storybook through Cypress tests. We have a decorator for our stories that need authentication to prompt to authenticate, and that decorator works fine in storybook alone, but as soon as we try to hit those stories with Cypress tests, then we get the hooks error and those stories fail. So that’s a new wrinkle. I will continue to investigate and see if I can figure out why running inside the Cypress runner iframe causes the rendering and hooks to behave differently when just running normally.

Switching our decorator from <Story /> to {Story()} did not help in this case

For us the issue started on storybook 7.0.2, when we upgraded @storybook/builder-vite and @storybook/react-vite from 7.0.2 to 7.0.7. Reverting these two back solved the issue.

I have reproduced this issue today. https://github.com/marybeshaw/storybook-issue-15223

@seppzero reproduced this issue a year ago: https://github.com/seppzero/sb-decorator-issue.

@shilman is there anything else you need to take off the “needs reproduction” tag?

I just enabled reactOptions.strictMode and hitting this error as well.

Seems to come from a combination of reactOptions.strictMode, a preview.js decorator, and addons. I can make it go away by either removing the decorator, or disabling @storybook/addon-essentials.

Will try to make repro.

Seeing the same problem now on 7.4.4 -> Had no problems on 7.4.2 before updating

Hey, the workarounds listed here don’t work. I’m hitting this with latest SB version (7.0.23), when is 7.1.0 going to be released?

Thanks for the reminder @mcrider. I can’t push to @redbugz’s branch so I’ll just need to make a new PR from my branch I guess.

We just also run in that issue.

@shilman here is a reproduction-repo based on sb@next init https://github.com/seppzero/sb-decorator-issue. I hope that helps.

Can you please create a reproduction by running npx sb@next repro, following the instructions, and linking it in your issue description? We prioritize issues with reproductions over those without. Thank you! 🙏