Skip to content

Commit b627269

Browse files
committed
Ensure consistent state in mapState
Closes #86
1 parent 661e643 commit b627269

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

src/components/createConnect.js

+29-28
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,30 @@ export default function createConnect(React) {
8989
};
9090

9191
shouldComponentUpdate(nextProps, nextState) {
92-
return !pure || !shallowEqual(this.state.props, nextState.props);
92+
if (!pure) {
93+
this.updateState(nextProps);
94+
return true;
95+
}
96+
97+
const storeChanged = nextState.storeState !== this.state.storeState;
98+
const propsChanged = !shallowEqual(nextProps, this.props);
99+
let mapStateProducedChange = false;
100+
let dispatchPropsChanged = false;
101+
102+
if (storeChanged || (propsChanged && shouldUpdateStateProps)) {
103+
mapStateProducedChange = this.updateStateProps(nextProps);
104+
}
105+
106+
if (propsChanged && shouldUpdateDispatchProps) {
107+
dispatchPropsChanged = this.updateDispatchProps(nextProps);
108+
}
109+
110+
if (propsChanged || mapStateProducedChange || dispatchPropsChanged) {
111+
this.updateState(nextProps);
112+
return true;
113+
}
114+
115+
return false;
93116
}
94117

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

107130
this.stateProps = computeStateProps(this.store, props);
108131
this.dispatchProps = computeDispatchProps(this.store, props);
109-
this.state = {
110-
props: this.computeNextState()
111-
};
132+
this.state = { storeState: null };
133+
this.updateState();
112134
}
113135

114136
computeNextState(props = this.props) {
@@ -140,12 +162,7 @@ export default function createConnect(React) {
140162
}
141163

142164
updateState(props = this.props) {
143-
const nextState = this.computeNextState(props);
144-
if (!shallowEqual(nextState, this.state.props)) {
145-
this.setState({
146-
props: nextState
147-
});
148-
}
165+
this.nextState = this.computeNextState(props);
149166
}
150167

151168
isSubscribed() {
@@ -170,20 +187,6 @@ export default function createConnect(React) {
170187
this.trySubscribe();
171188
}
172189

173-
componentWillReceiveProps(nextProps) {
174-
if (!shallowEqual(nextProps, this.props)) {
175-
if (shouldUpdateStateProps) {
176-
this.updateStateProps(nextProps);
177-
}
178-
179-
if (shouldUpdateDispatchProps) {
180-
this.updateDispatchProps(nextProps);
181-
}
182-
183-
this.updateState(nextProps);
184-
}
185-
}
186-
187190
componentWillUnmount() {
188191
this.tryUnsubscribe();
189192
}
@@ -193,9 +196,7 @@ export default function createConnect(React) {
193196
return;
194197
}
195198

196-
if (this.updateStateProps()) {
197-
this.updateState();
198-
}
199+
this.setState({storeState: this.store.getState()});
199200
}
200201

201202
getWrappedInstance() {
@@ -205,7 +206,7 @@ export default function createConnect(React) {
205206
render() {
206207
return (
207208
<WrappedComponent ref='wrappedInstance'
208-
{...this.state.props} />
209+
{...this.nextState} />
209210
);
210211
}
211212
}

0 commit comments

Comments
 (0)