question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[React Hooks] Webpack transpiled component throws invariant during SSR

See original GitHub issue

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); } })));
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:28 (5 by maintainers)

github_iconTop GitHub Comments

27reactions
hilkeheremanscommented, Oct 25, 2018

Your code works fine here. You sure you are using both react and react-dom’s latest alpha versions? I forgot to update react-dom initially which resulted in some oddish errors.

19reactions
seedencommented, Dec 15, 2018

It is still not working with latest versions 😦

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found