Skip to content

Commit 1cc936e

Browse files
ynonptimdorr
authored andcommitted
warn on duplicate props (reduxjs#508)
1 parent 05ae26e commit 1cc936e

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/connect/mergeProps.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
import shallowEqual from '../utils/shallowEqual'
22
import verifyPlainObject from '../utils/verifyPlainObject'
3+
import warning from '../utils/warning'
34

45
export function defaultMergeProps(stateProps, dispatchProps, ownProps) {
5-
return { ...ownProps, ...stateProps, ...dispatchProps }
6+
if (process.env.NODE_ENV !== 'production') {
7+
const stateKeys = Object.keys(stateProps)
8+
9+
for ( let key of stateKeys ) {
10+
if (typeof ownProps[key] !== 'undefined') {
11+
warning(false, `Duplicate key ${key} sent from both parent and state`)
12+
break
13+
}
14+
}
15+
}
16+
17+
return {
18+
...ownProps,
19+
...stateProps,
20+
...dispatchProps
21+
}
622
}
723

824
export function wrapMergePropsFunc(mergeProps) {

test/components/connect.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,32 @@ describe('React', () => {
7979
expect(container.context.store).toBe(store)
8080
})
8181

82+
83+
it('should warn if same key is used in state and props', () => {
84+
const store = createStore(() => ({
85+
abc: 'bar'
86+
}))
87+
88+
@connect(({ abc }) => ({ abc }))
89+
class Container extends Component {
90+
render() {
91+
return <Passthrough {...this.props} />
92+
}
93+
}
94+
95+
const errorSpy = expect.spyOn(console, 'error')
96+
97+
TestUtils.renderIntoDocument(
98+
<ProviderMock store={store}>
99+
<Container abc="buz" />
100+
</ProviderMock>
101+
)
102+
errorSpy.destroy()
103+
expect(errorSpy).toHaveBeenCalled()
104+
})
105+
106+
107+
82108
it('should pass state and props to the given component', () => {
83109
const store = createStore(() => ({
84110
foo: 'bar',

0 commit comments

Comments
 (0)