react-hot-loader: HotLoader won't work after adding >=2 levels of HoC.
Way to reproduce the issue:
- Using this example (
nextbranch) https://github.com/gaearon/react-hot-boilerplate/tree/next - Add
src/HoC.js
import React, { Component } from 'react';
export function provideHoC() {
return WrappedComponent => {
class HoC extends Component {
render() {
return <WrappedComponent {...this.props} />
}
}
HoC.displayName = (WrappedComponent.displayName || WrappedComponent.name) + 'WithHOC';
return HoC;
};
}
- Modify
src/Counter.js
import React, { Component } from 'react';
+import { provideHoC } from './HoC';
-export default class Counter extends Component {
+class Counter extends Component {
constructor(props) {
super(props);
this.state = { counter: 0 };
}
componentDidMount() {
this.interval = setInterval(this.tick.bind(this), 1000);
}
tick() {
this.setState({ counter: this.state.counter + 1 });
}
componentWillUnmount() {
clearInterval(this.interval);
}
render() {
return <h2>Counter: {this.state.counter}</h2>;
}
}
+
+export default provideHoC()(provideHoC()(Counter));
npm startand go tohttp://localhost:3000.- Modify some text inside
rendermethod ofCounter. - The counter state will be gone.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 1
- Comments: 25
Hard to fix on huge projects with tons of nested HoCs (since it is proper way to do HoCs). I created babel plugin to solve this: https://www.npmjs.com/package/extract-hoc
Due to internal logic, all
spare parts(ie hot-replaceable) must be extracted as top-level variables, or HRL will not be able tohot-swapthem. This is a stopper for HoC or decorators. It is just easier to convert HOCs to a normal, prop-based components, and usereact, notfunctionalcomposition, to create target logic. Or just useA long issue finally fixed in React Hot Loader v4.
I added some tests here: https://github.com/quangbuule/extract-hoc/blob/master/test/__snapshots__/babel.test.js.snap
Here are the errors I get:
Still running into this issue, even after using extra-hoc babel plugin and manually extracting HoCs into separate components
Package.json: https://github.com/blazing-edge-labs/admin-playground/blob/master/package.json Component with HoCs: https://github.com/blazing-edge-labs/admin-playground/blob/master/src/modules/Examples/Profile/Edit/index.js
Decorators will not work at all. There is no way to make them work. Except #711 - it will literally solve everything. I’ll do my best to make v4 sorted out before Christmas.
https://github.com/quangbuule/extract-hoc solved this issue for me for react-apollo and react-redux HOCs. Would be great if this was integrated somehow.