You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unable to write a functional component using hooks (Error: Invalid hook call) #265
When copying the first snippet from the usage section and replace the provided code by the one in the gif from the top of the README, I get an "Error: Invalid hook call".
@adiyathemighty and I have spent some time investigating this today and can say that this happens because of a React version mismatch (project running react@17 while react-live runs react@16.8), as hinted in the error. However, this only seems to happen when server-side rendering is involved (as the error happens on the server, and not on the client). Indeed, it cannot be reproduced on CodeSandbox (which uses CSR only).
It can be reproduced with a barebone Next app though:
Running npm run dev (the development server with server-side rendering) yields the invalid hook call error. This is because Next uses react@17 while react-live@2.3 uses react@16.8.0.
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 (/Users/kitty/Sites/react-live-test/node_modules/react-live/node_modules/react/cjs/react.development.js:1465:13)
at Object.useState (/Users/kitty/Sites/react-live-test/node_modules/react-live/node_modules/react/cjs/react.development.js:1496:20)
at LiveProvider (/Users/kitty/Sites/react-live-test/node_modules/react-live/dist/react-live.cjs.js:344:25)
There doesn’t seem to be an easy workaround beyond downgrading react-live to 2.2.3, the latest version compatible with React 17 + SSR. Any further version will cause an error until react-live relies on React 17 as well instead of 16.8.0.
Activity
KittyGiraudel commentedon Aug 11, 2021
Hey there! 👋
@adiyathemighty and I have spent some time investigating this today and can say that this happens because of a React version mismatch (project running
react@17while react-live runsreact@16.8), as hinted in the error. However, this only seems to happen when server-side rendering is involved (as the error happens on the server, and not on the client). Indeed, it cannot be reproduced on CodeSandbox (which uses CSR only).It can be reproduced with a barebone Next app though:
In
pages/index.js, set up the example shared in the README:Running
npm run dev(the development server with server-side rendering) yields the invalid hook call error. This is because Next usesreact@17whilereact-live@2.3usesreact@16.8.0.There doesn’t seem to be an easy workaround beyond downgrading react-live to 2.2.3, the latest version compatible with React 17 + SSR. Any further version will cause an error until react-live relies on React 17 as well instead of 16.8.0.
arifszn commentedon Aug 20, 2021
Same issue for docusaurus with react 17.
jpdriver commentedon Aug 20, 2021
thank you so much for the investigation + writeup @KittyGiraudel
we should definitely be able to fix this -- i'd like to get
react@17included in the next round of dependency upgradesfor what it's worth, the demo site (also using Next) had a similar issue. I had to add these lines to the webpack config to solve it..
it might be worth seeing if that works as a temporary workaround so you can stay on
react-live@latestin the meantime?adithemighty commentedon Aug 23, 2021
Hello @jpdriver,
thanks for coming back to us with a workaround! I can confirm that it works :)
konsumer commentedon Sep 19, 2021
I am using
react-live@latestand get the same error with these versions (with fresh node_modules):testing with this react:
putting this in my next.config.js also didn't fix it:
If I downgade:
that got it running.
equinusocio commentedon Dec 17, 2021
Same, we had to downgrade to
react-live@2.2.3on Nextjs 12.0.7scottianstewart commentedon Feb 20, 2023
This appears to be fixed with the latest version of react-live 3.1.1
tujoworker commentedon Aug 5, 2026
Here is a new PR that adds SSR support.