Skip to content

Commit

Permalink
Ensure consistent state in mapState
Browse files Browse the repository at this point in the history
Closes reduxjs#86
  • Loading branch information
esamattis committed Sep 14, 2015
1 parent 661e643 commit ada28db
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions src/components/createConnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,30 @@ export default function createConnect(React) {
};

shouldComponentUpdate(nextProps, nextState) {
return !pure || !shallowEqual(this.state.props, nextState.props);
if (!pure) {
this.updateState(nextProps);
return true;
}

const storeChanged = nextState.storeState !== this.state.storeState;
const propsChanged = !shallowEqual(nextProps, this.props);
let mapStateProducedChange = false;
let dispatchPropsChanged = false;

if (storeChanged || (propsChanged && shouldUpdateStateProps)) {
mapStateProducedChange = this.updateStateProps(nextProps);
}

if (propsChanged && shouldUpdateDispatchProps) {
dispatchPropsChanged = this.updateDispatchProps(nextProps);
}

if (propsChanged || mapStateProducedChange || dispatchPropsChanged) {
this.updateState(nextProps);
return true;
} else {
return false;
}
}

constructor(props, context) {
Expand All @@ -106,9 +129,8 @@ export default function createConnect(React) {

this.stateProps = computeStateProps(this.store, props);
this.dispatchProps = computeDispatchProps(this.store, props);
this.state = {
props: this.computeNextState()
};
this.state = { storeState: null };
this.updateState();
}

computeNextState(props = this.props) {
Expand Down Expand Up @@ -140,12 +162,7 @@ export default function createConnect(React) {
}

updateState(props = this.props) {
const nextState = this.computeNextState(props);
if (!shallowEqual(nextState, this.state.props)) {
this.setState({
props: nextState
});
}
this.nextState = this.computeNextState(props);
}

isSubscribed() {
Expand All @@ -170,20 +187,6 @@ export default function createConnect(React) {
this.trySubscribe();
}

componentWillReceiveProps(nextProps) {
if (!shallowEqual(nextProps, this.props)) {
if (shouldUpdateStateProps) {
this.updateStateProps(nextProps);
}

if (shouldUpdateDispatchProps) {
this.updateDispatchProps(nextProps);
}

this.updateState(nextProps);
}
}

componentWillUnmount() {
this.tryUnsubscribe();
}
Expand All @@ -193,9 +196,7 @@ export default function createConnect(React) {
return;
}

if (this.updateStateProps()) {
this.updateState();
}
this.setState({storeState: this.store.getState()});
}

getWrappedInstance() {
Expand All @@ -205,7 +206,7 @@ export default function createConnect(React) {
render() {
return (
<WrappedComponent ref='wrappedInstance'
{...this.state.props} />
{...this.nextState} />
);
}
}
Expand Down

0 comments on commit ada28db

Please sign in to comment.