Skip to content

Commit 0dd66fa

Browse files
committed
Re-enable storeKey
Fixes reduxjs#1393
1 parent 5b268f3 commit 0dd66fa

File tree

2 files changed

+39
-34
lines changed

2 files changed

+39
-34
lines changed

src/components/connectAdvanced.js

+2-15
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ export default function connectAdvanced(
8080
// determines whether this HOC subscribes to store changes
8181
shouldHandleStateChanges = true,
8282

83-
// REMOVED: the key of props/context to get the store
8483
storeKey = 'store',
8584

8685
// REMOVED: expose the wrapped component via refs
@@ -106,18 +105,6 @@ export default function connectAdvanced(
106105
'withRef is removed. To access the wrapped instance, use a ref on the connected component'
107106
)
108107

109-
const customStoreWarningMessage =
110-
'To use a custom Redux store for specific components, create a custom React context with ' +
111-
"React.createContext(), and pass the context object to React Redux's Provider and specific components" +
112-
' like: <Provider context={MyContext}><ConnectedComponent context={MyContext} /></Provider>. ' +
113-
'You may also pass a {context : MyContext} option to connect'
114-
115-
invariant(
116-
storeKey === 'store',
117-
'storeKey has been removed and does not do anything. ' +
118-
customStoreWarningMessage
119-
)
120-
121108
const Context = context
122109

123110
return function wrapWithConnect(WrappedComponent) {
@@ -182,7 +169,7 @@ export default function connectAdvanced(
182169
const contextValue = useContext(ContextToUse)
183170

184171
// The store _must_ exist as either a prop or in context
185-
const didStoreComeFromProps = Boolean(props.store)
172+
const didStoreComeFromProps = Boolean(props[storeKey])
186173
const didStoreComeFromContext =
187174
Boolean(contextValue) && Boolean(contextValue.store)
188175

@@ -194,7 +181,7 @@ export default function connectAdvanced(
194181
`React context consumer to ${displayName} in connect options.`
195182
)
196183

197-
const store = props.store || contextValue.store
184+
const store = props[storeKey] || contextValue.store
198185

199186
const childPropsSelector = useMemo(() => {
200187
// The child props selector needs the store reference as an input.

test/components/connect.spec.js

+37-19
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,43 @@ describe('React', () => {
9999
expect(tester.getByTestId('hi')).toHaveTextContent('there')
100100
})
101101

102+
it('should receive the store state in the store prop', () => {
103+
const store = createStore(() => ({ hi: 'there' }))
104+
105+
@connect(state => state)
106+
class Container extends Component {
107+
render() {
108+
return <Passthrough {...this.props} />
109+
}
110+
}
111+
112+
const tester = rtl.render(<Container pass="through" store={store} />)
113+
114+
expect(tester.getByTestId('hi')).toHaveTextContent('there')
115+
})
116+
117+
it('should allow overriding the store prop with storeKey', () => {
118+
const store = createStore(() => ({ hi: 'there' }))
119+
120+
@connect(
121+
state => state,
122+
undefined,
123+
undefined,
124+
{ storeKey: 'reduxStore' }
125+
)
126+
class Container extends Component {
127+
render() {
128+
return <Passthrough {...this.props} />
129+
}
130+
}
131+
132+
const tester = rtl.render(
133+
<Container pass="through" reduxStore={store} />
134+
)
135+
136+
expect(tester.getByTestId('hi')).toHaveTextContent('there')
137+
})
138+
102139
it('should pass state and props to the given component', () => {
103140
const store = createStore(() => ({
104141
foo: 'bar',
@@ -2872,25 +2909,6 @@ describe('React', () => {
28722909
).toThrow(/withRef is removed/)
28732910
})
28742911

2875-
it('should error on receiving a custom store key', () => {
2876-
const connectOptions = { storeKey: 'customStoreKey' }
2877-
2878-
expect(() => {
2879-
@connect(
2880-
undefined,
2881-
undefined,
2882-
undefined,
2883-
connectOptions
2884-
)
2885-
class Container extends Component {
2886-
render() {
2887-
return <Passthrough {...this.props} />
2888-
}
2889-
}
2890-
new Container()
2891-
}).toThrow(/storeKey has been removed/)
2892-
})
2893-
28942912
it.skip('should error on custom store', () => {
28952913
function Comp() {
28962914
return <div>hi</div>

0 commit comments

Comments
 (0)