Skip to content

Commit 42f26b2

Browse files
mikecluckDidier Franc
authored andcommitted
Handle loading components in the background (#11)
* Added unmount check to prevent invalid force update * Made sure to handle the case of unmounting and remounting before loading completes
1 parent b650496 commit 42f26b2

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

dist/react-code-splitting.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,13 @@ var Async = function (_React$Component) {
3737

3838
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Async.__proto__ || Object.getPrototypeOf(Async)).call.apply(_ref, [this].concat(args))), _this), _this.componentWillMount = function () {
3939
_this.props.load.then(function (c) {
40-
_this.C = c;_this.forceUpdate();
40+
_this.C = c;
41+
if (!_this.cancelUpdate) {
42+
_this.forceUpdate();
43+
}
4144
});
45+
}, _this.componentWillUnmount = function () {
46+
_this.cancelUpdate = true;
4247
}, _this.render = function () {
4348
var componentProps = _this.props.componentProps;
4449

src/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@ import PropTypes from 'prop-types'
33

44
export default class Async extends React.Component {
55
componentWillMount = () => {
6-
this.props.load.then((c) => { this.C = c; this.forceUpdate() })
6+
this.cancelUpdate = false
7+
this.props.load.then((c) => {
8+
this.C = c
9+
if (!this.cancelUpdate) {
10+
this.forceUpdate()
11+
}
12+
})
13+
}
14+
15+
componentWillUnmount = () => {
16+
this.cancelUpdate = true
717
}
818

919
render = () => {

0 commit comments

Comments
 (0)