Skip to content

Commit 8f2e4c4

Browse files
committed
Allow non-Redux-store values as a prop named store
1 parent b832f83 commit 8f2e4c4

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/components/connectAdvanced.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,14 @@ export default function connectAdvanced(
163163
// Retrieve the store and ancestor subscription via context, if available
164164
const contextValue = useContext(ContextToUse)
165165

166-
// The store _must_ exist as either a prop or in context
167-
const didStoreComeFromProps = Boolean(props.store)
166+
// The store _must_ exist as either a prop or in context.
167+
// We'll check to see if it _looks_ like a Redux store first.
168+
// This allows us to pass through a `store` prop that is just a plain value.
169+
console.log('Store from props: ', props.store)
170+
const didStoreComeFromProps =
171+
Boolean(props.store) &&
172+
Boolean(props.store.getState) &&
173+
Boolean(props.store.dispatch)
168174
const didStoreComeFromContext =
169175
Boolean(contextValue) && Boolean(contextValue.store)
170176

@@ -176,7 +182,8 @@ export default function connectAdvanced(
176182
`React context consumer to ${displayName} in connect options.`
177183
)
178184

179-
const store = props.store || contextValue.store
185+
// Based on the previous check, one of these must be true
186+
const store = didStoreComeFromProps ? props.store : contextValue.store
180187

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

test/components/connect.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,21 @@ describe('React', () => {
20902090
expect(actualState).toEqual(expectedState)
20912091
})
20922092

2093+
it('should pass through a store prop that is not actually a Redux store', () => {
2094+
const notActuallyAStore = 42
2095+
2096+
const store = createStore(() => 123)
2097+
const Decorated = connect(state => ({ state }))(Passthrough)
2098+
2099+
const rendered = rtl.render(
2100+
<ProviderMock store={store}>
2101+
<Decorated store={notActuallyAStore} />
2102+
</ProviderMock>
2103+
)
2104+
2105+
expect(rendered.getByTestId('store')).toHaveTextContent('42')
2106+
})
2107+
20932108
it('should pass through ancestor subscription when store is given as a prop', () => {
20942109
const c3Spy = jest.fn()
20952110
const c2Spy = jest.fn()

0 commit comments

Comments
 (0)