Skip to content

Commit 0c05c01

Browse files
Martin Schmiedgaearon
Martin Schmied
authored andcommitted
Prevent mapState from running twice when component is bound
1 parent 74081e2 commit 0c05c01

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

src/components/connect.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
118118

119119
this.stateProps = computeStateProps(this.store, props)
120120
this.dispatchProps = computeDispatchProps(this.store, props)
121-
this.state = { storeState: null }
121+
this.state = { storeState: this.store.getState() }
122122
this.updateState()
123123
}
124124

test/components/connect.spec.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ describe('React', () => {
466466
outerComponent.setFoo('BAR')
467467
outerComponent.setFoo('DID')
468468

469-
expect(invocationCount).toEqual(2)
469+
expect(invocationCount).toEqual(1)
470470
})
471471

472472
it('should invoke mapState every time props are changed if it has zero arguments', () => {
@@ -562,7 +562,7 @@ describe('React', () => {
562562
outerComponent.setFoo('BAR')
563563
outerComponent.setFoo('BAZ')
564564

565-
expect(invocationCount).toEqual(4)
565+
expect(invocationCount).toEqual(3)
566566
expect(propsPassedIn).toEqual({
567567
foo: 'BAZ'
568568
})
@@ -813,12 +813,12 @@ describe('React', () => {
813813
div
814814
)
815815

816-
expect(mapStateToPropsCalls).toBe(2)
816+
expect(mapStateToPropsCalls).toBe(1)
817817
const spy = expect.spyOn(console, 'error')
818818
store.dispatch({ type: 'APPEND', body: 'a' })
819819
spy.destroy()
820820
expect(spy.calls.length).toBe(0)
821-
expect(mapStateToPropsCalls).toBe(2)
821+
expect(mapStateToPropsCalls).toBe(1)
822822
})
823823

824824
it('should shallowly compare the selected state to prevent unnecessary updates', () => {
@@ -1411,19 +1411,19 @@ describe('React', () => {
14111411
</ProviderMock>
14121412
)
14131413

1414-
expect(childMapStateInvokes).toBe(2)
1414+
expect(childMapStateInvokes).toBe(1)
14151415

14161416
// The store state stays consistent when setState calls are batched
14171417
ReactDOM.unstable_batchedUpdates(() => {
14181418
store.dispatch({ type: 'APPEND', body: 'c' })
14191419
})
1420-
expect(childMapStateInvokes).toBe(3)
1420+
expect(childMapStateInvokes).toBe(2)
14211421

14221422
// setState calls DOM handlers are batched
14231423
const container = TestUtils.findRenderedComponentWithType(tree, Container)
14241424
const node = container.getWrappedInstance().refs.button
14251425
TestUtils.Simulate.click(node)
1426-
expect(childMapStateInvokes).toBe(4)
1426+
expect(childMapStateInvokes).toBe(3)
14271427

14281428
// In future all setState calls will be batched[1]. Uncomment when it
14291429
// happens. For now redux-batched-updates middleware can be used as
@@ -1432,7 +1432,7 @@ describe('React', () => {
14321432
// [1]: https://twitter.com/sebmarkbage/status/642366976824864768
14331433
//
14341434
// store.dispatch({ type: 'APPEND', body: 'd' })
1435-
// expect(childMapStateInvokes).toBe(5)
1435+
// expect(childMapStateInvokes).toBe(4)
14361436
})
14371437

14381438
it('should not render the wrapped component when mapState does not produce change', () => {
@@ -1458,12 +1458,12 @@ describe('React', () => {
14581458
)
14591459

14601460
expect(renderCalls).toBe(1)
1461-
expect(mapStateCalls).toBe(2)
1461+
expect(mapStateCalls).toBe(1)
14621462

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

14651465
// After store a change mapState has been called
1466-
expect(mapStateCalls).toBe(3)
1466+
expect(mapStateCalls).toBe(2)
14671467
// But render is not because it did not make any actual changes
14681468
expect(renderCalls).toBe(1)
14691469
})

0 commit comments

Comments
 (0)