react: [React Hooks] Webpack transpiled component throws invariant during SSR
What 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); } })));
}
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 28 (4 by maintainers)
Your code works fine here. You sure you are using both
reactandreact-dom’s latest alpha versions? I forgot to updatereact-dominitially which resulted in some oddish errors.It is still not working with latest versions 😦
I am afraid, but hook would break any custom react-tree traversal:
I’ve having this same issue but not using SSR. Literally copy/pasted the
<Example />counter component into my existing app. Here’s the transpiled code.react@16.7.0-alpha.0 react-dom@16.7.0-alpha.0 webpack@4.22 react-hot-loader@4.3.11
Hooks should work with
renderToString. @pheuter are you using Babel with Webpack? If so, what plugins are you using?