Skip to content

Commit 781510b

Browse files
fix the issue with redundant updates due to late props comparison
1 parent 27a10dc commit 781510b

File tree

3 files changed

+10
-12
lines changed

3 files changed

+10
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## react-coroutine v1.0.4
2+
3+
* Fix the issue with redundant updates due to late props comparison
4+
15
## react-coroutine v1.0.3
26

37
* Use `jsnext:main` instead of `pkg.module` because of Webpack 2 issue

modules/CoroutineFactory.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ export default function create(asyncFn, getVariables = () => ({})) {
1818
const body = variables.placeholder || this.state.body;
1919
this.state = { body, variables };
2020
this.asyncFunction = asyncFn;
21-
}
22-
23-
componentWillReceiveProps(nextProps, nextContext) {
24-
const variables = getVariables(nextProps, nextContext);
25-
this.setState(() => { variables }, () => super.componentWillReceiveProps(nextProps));
21+
this.getVariables = getVariables;
2622
}
2723
}
2824
}

modules/CoroutineRender.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { Component } from 'react';
22
import isEqual from 'shallowequal';
33

4-
function shallowCompare(instance, nextProps, nextState) {
5-
return !isEqual(instance.props, nextProps) || !isEqual(instance.state, nextState);
6-
}
7-
84
export default class AsyncComponent extends Component {
95
constructor(props, context) {
106
super(props, context);
@@ -14,6 +10,7 @@ export default class AsyncComponent extends Component {
1410
this.forceUpdateHelper = this.forceUpdate.bind(this);
1511
this.isComponentMounted = false;
1612
this.asyncFunction = props.co;
13+
this.getVariables = props.getVariables || (() => props.variables || {});
1714
}
1815

1916
forceUpdate(variables = this.state.variables, props = this.props) {
@@ -52,14 +49,15 @@ export default class AsyncComponent extends Component {
5249
return this.forceUpdate();
5350
}
5451

55-
componentWillReceiveProps(nextProps) {
56-
if (shallowCompare(this, nextProps)) {
52+
componentWillReceiveProps(nextProps, nextContext) {
53+
if (!isEqual(this, nextProps)) {
5754
if (this.iterator && this.iterator.return) {
5855
this.iterator.return();
5956
}
6057

6158
if (this.isComponentMounted) {
62-
this.forceUpdate(this.state.variables, nextProps);
59+
const variables = this.getVariables(nextProps, nextContext);
60+
this.forceUpdate(variables, nextProps);
6361
}
6462
}
6563
}

0 commit comments

Comments
 (0)