[React Hooks] Webpack transpiled component throws invariant during SSR
See original GitHub issueWhat is the current behavior?
UnhandledPromiseRejectionWarning: Invariant Violation: Hooks can only be called inside the body of a function component.
| at invariant (webpack://%5Bname%5D_%5Bhash%5D/./node_modules/react/cjs/react.development.js?:125:15)
| at resolveDispatcher (webpack://%5Bname%5D_%5Bhash%5D/./node_modules/react/cjs/react.development.js?:1450:28)
| at useState (webpack://%5Bname%5D_%5Bhash%5D/./node_modules/react/cjs/react.development.js?:1473:20)
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React? 16.7.0-alpha.0
This is what the component looks like after it’s transpiled by Webpack:
function HookedComponent() {
var _a = Object(react__WEBPACK_IMPORTED_MODULE_1__["useState"])("Mary"), name = _a[0], setName = _a[1];
return (react__WEBPACK_IMPORTED_MODULE_1__["createElement"]("div", null,
react__WEBPACK_IMPORTED_MODULE_1__["createElement"]("h1", null, name),
react__WEBPACK_IMPORTED_MODULE_1__["createElement"]("input", { value: name, onChange: function (e) { return setName(e.target.value); } })));
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:28 (5 by maintainers)
Top Results From Across the Web
Cant use Hooks with my React Component Library. Invariant ...
When I build yarn webpack --mode development it throws 'require is not defined'. Here it goes a GitHub repo with my complete code...
Read more >Intro to React Server Side Rendering | by Suhan Wijaya
[A] This tells webpack to transpile ES6+ features into JS code that's natively supported in Node.js and (most modern) browsers. Read the docs ......
Read more >Tag: Server-side Rendering - somewhat abstract
Well, React is rendering the client application in a virtual DOM and comparing it to the content it is trying to hydrate in...
Read more >React Redux Interview Questions and Answers (2022) - Adaface
General · How is Stateless component different from a Stateful component? · What is the difference between state and props? · What can...
Read more >Solving React Hooks' invalid hook call warning - Rob Kendal
Now for the real meat and potatoes of the solution: we need to configure Webpack to essentially ignore React as part of the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Your code works fine here. You sure you are using both
react
andreact-dom
’s latest alpha versions? I forgot to updatereact-dom
initially which resulted in some oddish errors.It is still not working with latest versions 😦