Skip to content

Commit c20be4f

Browse files
committed
Fix issues with stale props
Closes reduxjs#86
1 parent c2f7166 commit c20be4f

File tree

1 file changed

+19
-52
lines changed

1 file changed

+19
-52
lines changed

src/components/createConnect.js

+19-52
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ export default function createConnect(React) {
3737
// Helps track hot reloading.
3838
const version = nextVersion++;
3939

40-
function computeStateProps(store, props) {
41-
const state = store.getState();
40+
function computeStateProps(storeState, props) {
4241
const stateProps = shouldUpdateStateProps ?
43-
finalMapStateToProps(state, props) :
44-
finalMapStateToProps(state);
42+
finalMapStateToProps(storeState, props) :
43+
finalMapStateToProps(storeState);
4544

4645
invariant(
4746
isPlainObject(stateProps),
@@ -89,7 +88,8 @@ export default function createConnect(React) {
8988
};
9089

9190
shouldComponentUpdate(nextProps, nextState) {
92-
return !pure || !shallowEqual(this.state.props, nextState.props);
91+
return !pure || !shallowEqual(this.props, nextProps) ||
92+
!shallowEqual(this.state, nextState);
9393
}
9494

9595
constructor(props, context) {
@@ -104,48 +104,17 @@ export default function createConnect(React) {
104104
`or explicitly pass "store" as a prop to "${this.constructor.displayName}".`
105105
);
106106

107-
this.stateProps = computeStateProps(this.store, props);
108-
this.dispatchProps = computeDispatchProps(this.store, props);
109107
this.state = {
110-
props: this.computeNextState()
108+
dispatchProps: computeDispatchProps(this.store, props),
109+
storeState: this.store.getState()
111110
};
112111
}
113112

114-
computeNextState(props = this.props) {
115-
return computeNextState(
116-
this.stateProps,
117-
this.dispatchProps,
118-
props
119-
);
120-
}
121-
122-
updateStateProps(props = this.props) {
123-
const nextStateProps = computeStateProps(this.store, props);
124-
if (shallowEqual(nextStateProps, this.stateProps)) {
125-
return false;
126-
}
127-
128-
this.stateProps = nextStateProps;
129-
return true;
130-
}
131113

132114
updateDispatchProps(props = this.props) {
133-
const nextDispatchProps = computeDispatchProps(this.store, props);
134-
if (shallowEqual(nextDispatchProps, this.dispatchProps)) {
135-
return false;
136-
}
137-
138-
this.dispatchProps = nextDispatchProps;
139-
return true;
140-
}
141-
142-
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-
}
115+
this.setState({
116+
dispatchProps: computeDispatchProps(this.store, props)
117+
});
149118
}
150119

151120
isSubscribed() {
@@ -172,15 +141,10 @@ export default function createConnect(React) {
172141

173142
componentWillReceiveProps(nextProps) {
174143
if (!shallowEqual(nextProps, this.props)) {
175-
if (shouldUpdateStateProps) {
176-
this.updateStateProps(nextProps);
177-
}
178-
179144
if (shouldUpdateDispatchProps) {
180145
this.updateDispatchProps(nextProps);
181146
}
182147

183-
this.updateState(nextProps);
184148
}
185149
}
186150

@@ -193,19 +157,24 @@ export default function createConnect(React) {
193157
return;
194158
}
195159

196-
if (this.updateStateProps()) {
197-
this.updateState();
198-
}
160+
this.setState({storeState: this.store.getState()});
199161
}
200162

201163
getWrappedInstance() {
202164
return this.refs.wrappedInstance;
203165
}
204166

205167
render() {
168+
169+
const finalProps = computeNextState(
170+
computeStateProps(this.state.storeState, this.props),
171+
this.state.dispatchProps,
172+
this.props
173+
);
174+
206175
return (
207176
<WrappedComponent ref='wrappedInstance'
208-
{...this.state.props} />
177+
{...finalProps} />
209178
);
210179
}
211180
}
@@ -221,9 +190,7 @@ export default function createConnect(React) {
221190

222191
// Update the state and bindings.
223192
this.trySubscribe();
224-
this.updateStateProps();
225193
this.updateDispatchProps();
226-
this.updateState();
227194
};
228195
}
229196

0 commit comments

Comments
 (0)