storybook: [Bug] "Invalid hook call" error after upgrading to Storybook 6
Describe the bug
I am updating Storybook to version 6 in my project. After making some minor adjustments, such as installing two missing @types libraries, I believed that I wouldn’t need to do anything else, as the Storybook would work.
Steps to reproduce the behavior
- Clone the repo https://github.com/natura-cosmeticos/natds-js/tree/renovate/major-storybook-monorepo;
- Switch to the branch
renovate/major-storybook-monorepo
; - Run
yarn install
to install all dependencies; - Go to
packages/web
and, if needed, runyarn install
again; - Run
yarn run storybook:start
; - Open http://localhost:5000/ (if needed);
- Open Developer Tools and look for console messages.
I also tried to delete all files inside of .storybook
and keep an empty main.js
, and it still does not work.
Expected behavior
The Storybook application should load, even if warnings or errors appear on the console.
Screenshots
System
Environment Info:
System:
OS: macOS 10.15.5
CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
Binaries:
Node: 12.17.0 - /usr/local/bin/node
Yarn: 1.22.4 - ~/GitHub/natura/natds-js/node_modules/.bin/yarn
npm: 6.14.4 - /usr/local/bin/npm
Browsers:
Chrome: 86.0.4221.3
Firefox: 78.0.1
Safari: 13.1.1
Additional context
Trace
Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem.
at resolveDispatcher (react.development.js:1431)
at Object.useContext (react.development.js:1439)
at useTheme (emotion-theming.browser.esm.js:112)
at useMenu (vendors~main.62fe39d39e82bef806a4.bundle.js:107725)
at Object.mapper [as current] (vendors~main.62fe39d39e82bef806a4.bundle.js:105271)
at ManagerConsumer (vendors~main.62fe39d39e82bef806a4.bundle.js:143113)
at renderWithHooks (vendors~main.62fe39d39e82bef806a4.bundle.js:20836)
at mountIndeterminateComponent (vendors~main.62fe39d39e82bef806a4.bundle.js:23515)
at beginWork (vendors~main.62fe39d39e82bef806a4.bundle.js:24629)
at HTMLUnknownElement.callCallback (vendors~main.62fe39d39e82bef806a4.bundle.js:6221)
vendors~main.62fe39d39e82bef806a4.bundle.js:25560 The above error occurred in the <ManagerConsumer> component:
in ManagerConsumer
in Unknown (created by Layout)
in div
in Styled(div) (created by Sidebar)
in Sidebar (created by Layout)
in Layout
in WithTheme(Layout)
in Unknown
in div
in Styled(div)
in Unknown (created by SizeMeRenderer(Component))
in SizeMeReferenceWrapper (created by SizeMeRenderer(Component))
in SizeMeRenderer(Component) (created by SizeMe(Component))
in SizeMe(Component)
in ThemeProvider
in Unknown (created by ManagerConsumer)
in ManagerConsumer (created by Manager)
in EffectOnMount (created by Manager)
in Manager (created by Context.Consumer)
in Location (created by QueryLocation)
in QueryLocation (created by Root)
in LocationProvider (created by Root)
in HelmetProvider (created by Root)
in Root
React will try to recreate this component tree from scratch using the error boundary you provided, LocationProvider.
logCapturedError @ vendors~main.62fe39d39e82bef806a4.bundle.js:25560
vendors~main.62fe39d39e82bef806a4.bundle.js:4168 Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem.
at resolveDispatcher (react.development.js:1431)
at Object.useContext (react.development.js:1439)
at useTheme (emotion-theming.browser.esm.js:112)
at useMenu (vendors~main.62fe39d39e82bef806a4.bundle.js:107725)
at Object.mapper [as current] (vendors~main.62fe39d39e82bef806a4.bundle.js:105271)
at ManagerConsumer (vendors~main.62fe39d39e82bef806a4.bundle.js:143113)
at renderWithHooks (vendors~main.62fe39d39e82bef806a4.bundle.js:20836)
at mountIndeterminateComponent (vendors~main.62fe39d39e82bef806a4.bundle.js:23515)
at beginWork (vendors~main.62fe39d39e82bef806a4.bundle.js:24629)
at HTMLUnknownElement.callCallback (vendors~main.62fe39d39e82bef806a4.bundle.js:6221)
vendors~main.62fe39d39e82bef806a4.bundle.js:25560 The above error occurred in the <LocationProvider> component:
in LocationProvider (created by Root)
in HelmetProvider (created by Root)
in Root
Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://fb.me/react-error-boundaries to learn more about error boundaries.
logCapturedError @ vendors~main.62fe39d39e82bef806a4.bundle.js:25560
vendors~main.62fe39d39e82bef806a4.bundle.js:17135 Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem.
at resolveDispatcher (react.development.js:1431)
at Object.useContext (react.development.js:1439)
at useTheme (emotion-theming.browser.esm.js:112)
at useMenu (vendors~main.62fe39d39e82bef806a4.bundle.js:107725)
at Object.mapper [as current] (vendors~main.62fe39d39e82bef806a4.bundle.js:105271)
at ManagerConsumer (vendors~main.62fe39d39e82bef806a4.bundle.js:143113)
at renderWithHooks (vendors~main.62fe39d39e82bef806a4.bundle.js:20836)
at mountIndeterminateComponent (vendors~main.62fe39d39e82bef806a4.bundle.js:23515)
at beginWork (vendors~main.62fe39d39e82bef806a4.bundle.js:24629)
at HTMLUnknownElement.callCallback (vendors~main.62fe39d39e82bef806a4.bundle.js:6221)
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 56
- Comments: 54 (22 by maintainers)
Commits related to this issue
- chore: add react and react-dom into the resolutions field ref. https://github.com/storybookjs/storybook/issues/11904 — committed to kufu/smarthr-ui by koba04 4 years ago
- add resolutions https://github.com/storybookjs/storybook/issues/11904 — committed to zendeskgarden/react-containers by hzhu 4 years ago
- Add resolutions https://github.com/storybookjs/storybook/issues/11904 — committed to zendeskgarden/react-containers by hzhu 4 years ago
- chore: add react and react-dom into the resolutions field ref. https://github.com/storybookjs/storybook/issues/11904 — committed to kufu/smarthr-ui by koba04 4 years ago
- chore: add react and react-dom into the resolutions field ref. https://github.com/storybookjs/storybook/issues/11904 — committed to kufu/smarthr-ui by koba04 4 years ago
- chore(deps): update Storybook to v6 (#952) * chore: npx sb@next upgrade * chore: add react and react-dom into the resolutions field ref. https://github.com/storybookjs/storybook/issues/11904 ... — committed to kufu/smarthr-ui by koba04 4 years ago
- fix invalid hook call - https://github.com/storybookjs/storybook/issues/11904 — committed to jizusun/generated-react-typescript-storybook by jizusun 4 years ago
It’s caused by multiple versions of React being loaded -
@storybook/ui
most likely contains its own version of React.Fixes:
yarn.lock
and runyarn
again. This should ensure the versions gets aligned.yarn why react
to ensure only one version existsWe are running into this issue as well. All the
@storybook
packages/addons are at6.0.4
. Deletingyarn.lock
fixes the issue, but that isn’t ideal.When I ran
yarn why react
I got the followingI then saw that Storybook brought in
16.8.3
and yarn added it as an extra entry in the lock file. I deleted version16.8.3
ofreact
andreact-dom
from the lock file and ranyarn install
. This fixed our issue!When running
yarn why react
again I got the followingAnother solution is to add the React Version you need in the
resolutions
property inpackage.json
Thank you @thebuilder for pointing me in the right direction!
Still not working using sb 6.0.7 and react 17.0.0-rc.0
I believe this is fixed in 6.1-alpha by https://github.com/storybookjs/storybook/pull/12874
and https://github.com/storybookjs/storybook/pull/12908
Please try it out and report back:
We’re currently working on getting these changes back into the stable release.
Hey everyone,
Now I was able to get Storybook working:
*storybook*
dependencies from mypackage.json
;node_modules
folder;react
andreact-dom
versions on mypackage.json
to16.13.1
;resolutions
to mypackage.json
:react
dependencies fromworkspaces.nohoist
on mypackage.json
;npx sb init
.So I’m gonna close this issue. Thank you all ❤️
Upgrading
@storybook/react
and@storybook/addon-knobs
to@next
fixed it for me just now.This worked like a charm for me.
I was following the Install Storybook and got this error.
package.json
here: https://github.com/jizusun/generated-react-typescript-storybook/blob/main/package.jsonRun the followings solved the problem.
Can confirm that this solved the problem for us. Storybook 6 here we come! 😄
I can confirm that deleting my
yarn.lock
file fixed this issue for me.Hi, I had the same issue, it’s a bit unexpected when you are excited discovering a quite new world (react + storybook) and after 5 minutes you are stuck in dependencies “deadlock”. The new version React 17 seems not to be compatible with Storybook 6.0 (latest stable version). I followed the recommendations and I upgraded to SB 6.1.0 alpha version and it works (how confident may I be to proceed with this weak dependency ?)!
Thanks for your support.
@jizusun’s solution worked for me, but I believe until this issue is fixed, this should be at least mentioned in the get started tutorial here: https://www.learnstorybook.com/intro-to-storybook/react/en/get-started/
This seems to be a great library, but the start sure is rough! Not being able to just start following the tutorial and waisting a lot of time looking for how to fix this issue is just plain sad.
I was getting a similar error, traced back to
react-textarea-autosize
:Sure enough, the latest version of
react-textarea-autosize
(8.2.0
) depends on react16.13.1
, leading to that third condition (3. You might have more than one copy of React in the same app
).Downgrading to react
16.13.1
fixed the error.EDIT: since I’m using Yarn, I just switched to forcing dependencies to user React 17 using resolutions (see the comment by @thebuilder)
I’ve also seen issues where there are multiple copies of
@storybook/*
installed because of some addon that depends on5.3
. This can cause a rendering error which gets the same message but has nothing to do withreact
versions. If you’re seeing this, also do annpm ls | grep storybook
and see if there are any mixed versions.See for example: https://github.com/storybookjs/storybook/issues/11373#issuecomment-672570880
Hi @shilman and @ZhengRui , I am also facing same error with SB v7 + react 18.2.0 +webpack5. Is there any solution for this issue.
--no-dll
works with React 17 as wellFair enough. I wanted to clarify for anyone who comes to this issue so that they don’t overfit, especially when it explicitly warns about multiple versions of React in the error.
Pretty sure the specific error in this issue, is caused by mismatching React versions. I got the same stacktrace trying to run Storybook after upgrading to 6.0, and aligning React solved it.
It fails to share the emotion theme context between different React versions.
The issue (#11373) you’re linking too has a different Stacktrace - properly also caused by mismatching dependencies.
Followed these instructions, with no effect. Still getting the same error.
You don’t have to delete your entire lock file.
Simply remove all dependencies that rely on React. See which ones by running
yarn why react
.Running
yarn why react
should only show one React version now.Install the dependencies again and yarn will resolve to the same React version (if they are compatible). Run
yarn why react
to verify that only one version is installed.Bonus, add this script to CI to make sure you don’t end up in this situation again:
@shilman thanks to the team for putting in that effort into the prerelease.
I just upgraded to it and my
create-react-library
project correctly serves Storybook in the browser. I’ve lockedreact
andreact-dom
to v16.13.1 though.I should attempt some tests with v17 at a later stage.
Simple reproduction: https://github.com/dburles/sb-react-bug. Also have the same issue with the latest alpha,
v6.1.0-alpha.10
@eric-burel we’re revamping a bunch of this stuff in 6.1, like https://github.com/storybookjs/storybook/pull/11628 and also removing the webpack DLLs which also cause lots of dependency sensitivity
Hi, I still see this issue after following all steps. This issue might worth being kept open? It is not really normal to have to set resolutions for SB to work.
(
Not related but congrats on theactually after digging it does not seems to be a page in Next meaning ^^)npx sb init
on Next, I have just noticed that it even create a demo story for pages!Edit: removed my error logs, issue is fixed for me eventually. To fix the issue, I needed to check also
yarn why react-dom
. I forgot that I had lock React version in the package.json (16.10.0
instead of^16.10.0
) so it installed both16.10.1
.Is there an explanation as to why Storybook is more sensitive to those mismatch nowadays?
phew @jimmyandrade ! glad you got it working!! 😅
Not sure if this is the same issue, but I’m also getting the
Invalid hook call
error viaEffectOnMount
.This was with react 17.0.0-rc.0, and downgrading to 16.13.1 resolved the issue.
@wtanna OK sorry for the false alarm. Just wanted to be sure because if some of the packages were out of date it could also lead to other problems aside from the one that this issue focuses on.
@wtanna did you use
npx sb upgrade
to upgrade? it looks like you might still have old versions of@storybook/*
packages?@thebuilder it’s caused by some class of errors in the rendering, not necessarily only multiple versions of React. The issue i mentioned above had a single version of React but multiple versions of
@storybook/components
.@shilman makes sense because I have a custom addon depending in 5.2.8 version, I’ll update and see what happens