Skip to content

Commit 778730f

Browse files
committed
Make all tests pass for big coonect refactoring! reduxjs#368
1 parent e9a874f commit 778730f

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/components/connect.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function tryCatch(fn, ctx) {
2424
try {
2525
return fn.apply(ctx)
2626
} catch (e) {
27+
console.error(e)
2728
errorObject.value = e
2829
return errorObject
2930
}
@@ -88,6 +89,7 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
8889
console.log("shouldComponentUpdate nextState.mergedPropsUpdated",nextState.mergedPropsUpdated)
8990
*/
9091
console.log("################## shouldComponentUpdate",nextState.mergedPropsUpdated)
92+
console.log("################## shouldComponentUpdate",nextState)
9193
return !pure || nextState.mergedPropsUpdated
9294
}
9395

@@ -250,6 +252,9 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
250252

251253

252254
// Fast track: bailout early because we don't need access to fresh props here
255+
console.log("isStateChange",isStateChange)
256+
console.log("pure",pure)
257+
console.log("!this.doStatePropsDependOnOwnProps",!this.doStatePropsDependOnOwnProps)
253258
if (isStateChange && pure && !this.doStatePropsDependOnOwnProps) {
254259
setStatePropsIfNeeded(this.props)
255260
if (!statePropsUpdated) {
@@ -259,8 +264,9 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
259264
}
260265

261266

262-
// Normal track: we precompute everything for shouldComponentUpdate, and try to reuse already done computation
263-
this.setState((previousState, currentProps) => {
267+
268+
const setStateFunction = (previousState, currentProps) => {
269+
264270
console.log("-> normal track with props=",currentProps)
265271

266272
setStatePropsIfNeeded(currentProps);
@@ -278,7 +284,6 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
278284
console.log("mergedProps",mergedProps)
279285
console.log("mergedPropsUpdated",mergedPropsUpdated)
280286

281-
282287
return {
283288
error: undefined,
284289
storeState,
@@ -287,7 +292,20 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
287292
mergedProps,
288293
mergedPropsUpdated
289294
}
295+
}
296+
297+
298+
299+
this.lastSetStateFunction = setStateFunction
300+
this.setState((previousState, currentProps) => {
301+
if ( this.lastSetStateFunction === setStateFunction ) {
302+
return setStateFunction(previousState,currentProps)
303+
}
304+
else {
305+
return previousState
306+
}
290307
});
308+
291309
}
292310

293311
getWrappedInstance() {

test/components/connect.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1482,7 +1482,7 @@ describe('React', () => {
14821482
expect(target.props.statefulValue).toEqual('bar')
14831483
})
14841484

1485-
it.only('should pass state consistently to mapState', () => {
1485+
it('should pass state consistently to mapState', () => {
14861486
const store = createStore(stringBuilder)
14871487

14881488
store.dispatch({ type: 'APPEND', body: 'a' })

0 commit comments

Comments
 (0)