Skip to content

Commit 1da657e

Browse files
committed
Formatting, add test case for mergeProps equality optimization
1 parent 4bb0087 commit 1da657e

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

src/components/connect.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
8282
if (!this.finalMapStateToProps) {
8383
return this.configureFinalMapState(store, props)
8484
}
85+
8586
const state = store.getState()
8687
const stateProps = this.doStatePropsDependOnOwnProps ?
8788
this.finalMapStateToProps(state, props) :
@@ -93,28 +94,38 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
9394
configureFinalMapState(store, props) {
9495
const mappedState = mapState(store.getState(), props)
9596
const isFactory = typeof mappedState === 'function'
97+
9698
this.finalMapStateToProps = isFactory ? mappedState : mapState
9799
this.doStatePropsDependOnOwnProps = this.finalMapStateToProps.length !== 1
98-
return isFactory ? this.computeStateProps(store, props) : checkStateShape(mappedState)
100+
101+
return isFactory ?
102+
this.computeStateProps(store, props) :
103+
checkStateShape(mappedState)
99104
}
100105

101106
computeDispatchProps(store, props) {
102107
if (!this.finalMapDispatchToProps) {
103108
return this.configureFinalMapDispatch(store, props)
104109
}
110+
105111
const { dispatch } = store
106112
const dispatchProps = this.doDispatchPropsDependOnOwnProps ?
107113
this.finalMapDispatchToProps(dispatch, props) :
108114
this.finalMapDispatchToProps(dispatch)
115+
109116
return checkStateShape(dispatchProps, true)
110117
}
111118

112119
configureFinalMapDispatch(store, props) {
113120
const mappedDispatch = mapDispatch(store.dispatch, props)
114121
const isFactory = typeof mappedDispatch === 'function'
122+
115123
this.finalMapDispatchToProps = isFactory ? mappedDispatch : mapDispatch
116124
this.doDispatchPropsDependOnOwnProps = this.finalMapDispatchToProps.length !== 1
117-
return isFactory ? this.computeDispatchProps(store, props) : checkStateShape(mappedDispatch, true)
125+
126+
return isFactory ?
127+
this.computeDispatchProps(store, props) :
128+
checkStateShape(mappedDispatch, true)
118129
}
119130

120131
updateStatePropsIfNeeded() {

test/components/connect.spec.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('React', () => {
3939
@connect()
4040
class Container extends Component {
4141
render() {
42-
return <div {...this.props} />
42+
return <Passthrough {...this.props} />
4343
}
4444
}
4545

@@ -1534,7 +1534,7 @@ describe('React', () => {
15341534
updatedCount++
15351535
}
15361536
render() {
1537-
return <div {...this.props} />
1537+
return <Passthrough {...this.props} />
15381538
}
15391539
}
15401540

@@ -1612,5 +1612,32 @@ describe('React', () => {
16121612
expect(memoizedReturnCount).toBe(2)
16131613
})
16141614

1615+
it('should not call update if mergeProps return value has not changed', () => {
1616+
let mapStateCalls = 0
1617+
let renderCalls = 0
1618+
const store = createStore(stringBuilder)
1619+
1620+
@connect(() => ({ a: ++mapStateCalls }), null, () => ({ changed: false }))
1621+
class Container extends Component {
1622+
render() {
1623+
renderCalls++
1624+
return <Passthrough {...this.props} />
1625+
}
1626+
}
1627+
1628+
TestUtils.renderIntoDocument(
1629+
<ProviderMock store={store}>
1630+
<Container />
1631+
</ProviderMock>
1632+
)
1633+
1634+
expect(renderCalls).toBe(1)
1635+
expect(mapStateCalls).toBe(1)
1636+
1637+
store.dispatch({ type: 'APPEND', body: 'a' })
1638+
1639+
expect(mapStateCalls).toBe(2)
1640+
expect(renderCalls).toBe(1)
1641+
})
16151642
})
16161643
})

0 commit comments

Comments
 (0)