Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-enable storeKey #1394

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 2 additions & 15 deletions src/components/connectAdvanced.js
Expand Up @@ -80,7 +80,6 @@ export default function connectAdvanced(
// determines whether this HOC subscribes to store changes
shouldHandleStateChanges = true,

// REMOVED: the key of props/context to get the store
storeKey = 'store',

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

const customStoreWarningMessage =
'To use a custom Redux store for specific components, create a custom React context with ' +
"React.createContext(), and pass the context object to React Redux's Provider and specific components" +
' like: <Provider context={MyContext}><ConnectedComponent context={MyContext} /></Provider>. ' +
'You may also pass a {context : MyContext} option to connect'

invariant(
storeKey === 'store',
'storeKey has been removed and does not do anything. ' +
customStoreWarningMessage
)

const Context = context

return function wrapWithConnect(WrappedComponent) {
Expand Down Expand Up @@ -182,7 +169,7 @@ export default function connectAdvanced(
const contextValue = useContext(ContextToUse)

// The store _must_ exist as either a prop or in context
const didStoreComeFromProps = Boolean(props.store)
const didStoreComeFromProps = Boolean(props[storeKey])
const didStoreComeFromContext =
Boolean(contextValue) && Boolean(contextValue.store)

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

const store = props.store || contextValue.store
const store = props[storeKey] || contextValue.store

const childPropsSelector = useMemo(() => {
// The child props selector needs the store reference as an input.
Expand Down
56 changes: 37 additions & 19 deletions test/components/connect.spec.js
Expand Up @@ -99,6 +99,43 @@ describe('React', () => {
expect(tester.getByTestId('hi')).toHaveTextContent('there')
})

it('should receive the store state in the store prop', () => {
const store = createStore(() => ({ hi: 'there' }))

@connect(state => state)
class Container extends Component {
render() {
return <Passthrough {...this.props} />
}
}

const tester = rtl.render(<Container pass="through" store={store} />)

expect(tester.getByTestId('hi')).toHaveTextContent('there')
})

it('should allow overriding the store prop with storeKey', () => {
const store = createStore(() => ({ hi: 'there' }))

@connect(
state => state,
undefined,
undefined,
{ storeKey: 'reduxStore' }
)
class Container extends Component {
render() {
return <Passthrough {...this.props} />
}
}

const tester = rtl.render(
<Container pass="through" reduxStore={store} />
)

expect(tester.getByTestId('hi')).toHaveTextContent('there')
})

it('should pass state and props to the given component', () => {
const store = createStore(() => ({
foo: 'bar',
Expand Down Expand Up @@ -2872,25 +2909,6 @@ describe('React', () => {
).toThrow(/withRef is removed/)
})

it('should error on receiving a custom store key', () => {
const connectOptions = { storeKey: 'customStoreKey' }

expect(() => {
@connect(
undefined,
undefined,
undefined,
connectOptions
)
class Container extends Component {
render() {
return <Passthrough {...this.props} />
}
}
new Container()
}).toThrow(/storeKey has been removed/)
})

it.skip('should error on custom store', () => {
function Comp() {
return <div>hi</div>
Expand Down