Skip to content

Commit 428f8b5

Browse files
lukeapagetimdorr
authored andcommitted
Allow 2nd method to get ownProps if factory doesn't require it. Fixes #604 (#616)
1 parent 0f5870e commit 428f8b5

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/connect/wrapMapToProps.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ export function wrapMapToPropsFunc(mapToProps, methodName) {
4343
: proxy.mapToProps(stateOrDispatch)
4444
}
4545

46-
proxy.dependsOnOwnProps = getDependsOnOwnProps(mapToProps)
46+
// allow detectFactoryAndVerify to get ownProps
47+
proxy.dependsOnOwnProps = true
4748

4849
proxy.mapToProps = function detectFactoryAndVerify(stateOrDispatch, ownProps) {
4950
proxy.mapToProps = mapToProps
51+
proxy.dependsOnOwnProps = getDependsOnOwnProps(mapToProps)
5052
let props = proxy(stateOrDispatch, ownProps)
5153

5254
if (typeof props === 'function') {

test/components/connect.spec.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,6 +1881,43 @@ describe('React', () => {
18811881
expect(memoizedReturnCount).toBe(2)
18821882
})
18831883

1884+
it('should allow a mapStateToProps factory consuming just state to return a function that gets ownProps', () => {
1885+
const store = createStore(() => ({ value: 1 }))
1886+
1887+
let initialState
1888+
let initialOwnProps
1889+
let secondaryOwnProps
1890+
const mapStateFactory = function (factoryInitialState) {
1891+
initialState = factoryInitialState
1892+
initialOwnProps = arguments[1];
1893+
return (state, props) => {
1894+
secondaryOwnProps = props
1895+
return { }
1896+
}
1897+
}
1898+
1899+
@connect(mapStateFactory)
1900+
class Container extends Component {
1901+
render() {
1902+
return <Passthrough {...this.props} />
1903+
}
1904+
}
1905+
1906+
TestUtils.renderIntoDocument(
1907+
<ProviderMock store={store}>
1908+
<div>
1909+
<Container name="a" />
1910+
</div>
1911+
</ProviderMock>
1912+
)
1913+
1914+
store.dispatch({ type: 'test' })
1915+
expect(initialOwnProps).toBe(undefined)
1916+
expect(initialState).toNotBe(undefined)
1917+
expect(secondaryOwnProps).toNotBe(undefined)
1918+
expect(secondaryOwnProps.name).toBe("a")
1919+
})
1920+
18841921
it('should allow providing a factory function to mapDispatchToProps', () => {
18851922
let updatedCount = 0
18861923
let memoizedReturnCount = 0
@@ -2134,7 +2171,7 @@ describe('React', () => {
21342171
class BlockUpdates extends Component {
21352172
shouldComponentUpdate() { return false; }
21362173
render() { return this.props.children; }
2137-
}
2174+
}
21382175

21392176
const mapStateToProps = expect.createSpy().andCall(state => ({ count: state }))
21402177
@connect(mapStateToProps)
@@ -2169,6 +2206,6 @@ describe('React', () => {
21692206

21702207
store.dispatch({ type: 'INC' })
21712208
})
2172-
2209+
21732210
})
21742211
})

0 commit comments

Comments
 (0)