react-hot-loader: [React Hooks] Incompatibility with react-hot-loader
Description
The new react hooks API does not work with react-hot-loader. Using the example in my app with export default hot(module)(App); at the bottom of app.js, the following error occurs:
Expected behavior
Should work.
Actual behavior
Uncaught Error: Hooks can only be called inside the body of a function component.
Reproducible Demo
Dunno of a demo is needed, just ping me if you need one.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 86
- Comments: 76 (12 by maintainers)
Links to this issue
Commits related to this issue
- WIP: Convert classes to functions using hooks N.B. Does not currently work in Gatsby development mode due to incompatibilities with react-hot-loader. See RHL issue 1088. https://github.com/gaearon/r... — committed to wKovacs64/drinks-gatsby by wKovacs64 6 years ago
- WIP: Convert classes to functions using hooks N.B. Does not currently work in Gatsby development mode due to incompatibilities with react-hot-loader. See RHL issue 1088. https://github.com/gaearon/r... — committed to wKovacs64/drinks-gatsby by wKovacs64 6 years ago
- WIP: Convert classes to functions using hooks N.B. Does not currently work in Gatsby development mode due to incompatibilities with react-hot-loader. See RHL issue 1088. https://github.com/gaearon/r... — committed to wKovacs64/drinks-gatsby by wKovacs64 6 years ago
- WIP: Convert classes to functions using hooks N.B. Does not currently work in Gatsby development mode due to incompatibilities with react-hot-loader. See RHL issue 1088. https://github.com/gaearon/r... — committed to wKovacs64/drinks-gatsby by wKovacs64 6 years ago
- WIP: Convert classes to functions using hooks N.B. Does not currently work in Gatsby development mode due to incompatibilities with react-hot-loader. See RHL issue 1088. https://github.com/gaearon/r... — committed to wKovacs64/drinks-gatsby by wKovacs64 6 years ago
- WIP: Convert classes to functions using hooks N.B. Does not currently work in Gatsby development mode due to incompatibilities with react-hot-loader. See RHL issue 1088. https://github.com/gaearon/r... — committed to wKovacs64/drinks-gatsby by wKovacs64 6 years ago
- WIP: Convert classes to functions using hooks N.B. Does not currently work in Gatsby development mode due to incompatibilities with react-hot-loader. See RHL issue 1088. https://github.com/gaearon/r... — committed to wKovacs64/drinks-gatsby by wKovacs64 6 years ago
- WIP: Convert classes to functions using hooks N.B. Does not currently work in Gatsby development mode due to incompatibilities with react-hot-loader. See RHL issue 1088. https://github.com/gaearon/r... — committed to wKovacs64/drinks-gatsby by wKovacs64 6 years ago
- WIP: Convert classes to functions using hooks N.B. Does not currently work in Gatsby development mode due to incompatibilities with react-hot-loader. See RHL issue 1088. https://github.com/gaearon/r... — committed to wKovacs64/drinks-gatsby by wKovacs64 6 years ago
- Replace unstated with Context and hooks HMR is not compatible with hooks currently. https://github.com/gaearon/react-hot-loader/issues/1088#issuecomment-433537974 — committed to matt-tingen/react-kindling by matt-tingen 6 years ago
- Re-enable HMR https://github.com/gaearon/react-hot-loader/issues/1088#issuecomment-446938770 — committed to matt-tingen/react-kindling by matt-tingen 6 years ago
Yes. RHL is 100% not compatible with hooks. There is just a few reasons behind it:
SFC are being converted to Class components. There is reason - to be able to
forceUpdateon HMR, as long there is no “update” method on SFC. I am looking for other way of forcing the update (like this. So RHL is killing SFC.“hotReplacementRender”. RHL is trying to do React’s job, and render the old and the new app, to merge them. So, obviously, that’s broken now.
I am going to draft a PR, to mitigate both problems. It will work, but not today.
4.6.0 just went out. No longer beta. Hooks should work out of the box without any setup needed
I just set
pureSFCand everything works fine, this can be a problem for other pieces @theKashey?There is a more proper fix, which would work my better and ever after - cold API
You may disable RHL for any custom type.
or just search for “useState/useEffect” inside component source code, and ❄️it.
Probably me. I already was used to introduce per-component settings to support React.memo, and the same would work for hooks. I’ll release a new version tomorrow, with memo and hooks 🤞, but, probably, without React.Lazy support.
4.5.2 released. All reported issues solved.
Meanwhile, everything depends on this - https://github.com/reactjs/rfcs/pull/74
Yes I’ve just seen the same. I tried removing my
hot(module)(App)equivalent but also required removing babel plugin to get hooks to work.“react-dom” does not support hooks. They are available in alpha-2, or 16.8
React-Hot-Loader is not quite CRA compatible. While we don’t have any good solution for CRA - just
setConfig({ pureSFC: true });. That’s enough for hooks to work (if you are using “next” version of RHL).Probably
pureSFCis a good option. Justhot- top component should be a component to be able to update nested tree on HMR.So - just don’t use “hidden” components (ie not exported as a top level variables) - and that would be ok. Also - there could be a problem with force update.
1 - look like
source-mapwent frontend way due tofetchpolyfill, probably. It’s fixed in0.8.0-beta.0, which I probably shall not use yet. See https://github.com/mozilla/source-map/issues/3492 - this error means that you have
ignoreSFC: true, but not@hot-loader/react-dom- it should display a message when hot-patches are detected.@theKashey the docs for hooks in the readme still seem old?
v4.5.3(or just next) released. False positive merges got fixed, among a set of other problems. 🤞 release candidate.
If I use
{ test: /\.jsx?$/, include: /node_modules/, use: ['react-hot-loader/webpack'], },as sad in readme I get multiply messages of save error: _If I try @hot-loader/react-dom alias I get:
until I’ll didn’t remove “react-hot-loader/babel” from .babelrc
@theKashey I am confused now 😦
What is the solution to hot-loading and hooks for
create-react-app?react-app-rewired?I really want to avoid to use a forked react-dom if possible.
Then we got all the cases (except npm) covered. I’ve updated react-🔥-dom, and included production version, as long @Bnaya hack could not be undone for prod builds.
the final configuration could be like:
Everything could work even without webpack-loader/react-hot-dom, but it could be strange experience, like React.memo could be not updated. (useMemo will be not updated in any case)
In my tests everything is working. What about you?
v4.5.0(next) solves most of 16.6/16.7 issues and could handle anything if you will got your node_modules processed by our webpack-loader. Please give a try and report back. PS: If someone could convert webpack loader to webpack plugin - that would be just great.