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

Show warning instead of throwing error that pure option has been removed #1922

Merged
merged 1 commit into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/components/connect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { mergePropsFactory } from '../connect/mergeProps'
import { createSubscription, Subscription } from '../utils/Subscription'
import { useIsomorphicLayoutEffect } from '../utils/useIsomorphicLayoutEffect'
import shallowEqual from '../utils/shallowEqual'
import warning from '../utils/warning'

import {
ReactReduxContext,
Expand Down Expand Up @@ -405,6 +406,8 @@ export interface Connect<DefaultState = unknown> {
// tslint:enable:no-unnecessary-generics
}

let hasWarnedAboutDeprecatedPureOption = false

/**
* Connects a React component to a Redux store.
*
Expand Down Expand Up @@ -452,8 +455,9 @@ function connect<
}: ConnectOptions<unknown, unknown, unknown, unknown> = {}
): unknown {
if (process.env.NODE_ENV !== 'production') {
if (pure !== undefined) {
throw new Error(
if (pure !== undefined && !hasWarnedAboutDeprecatedPureOption) {
hasWarnedAboutDeprecatedPureOption = true
warning(
'The `pure` option has been removed. `connect` is now always a "pure/memoized" component'
)
}
Expand Down
61 changes: 58 additions & 3 deletions test/components/connect.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2912,6 +2912,57 @@ describe('React', () => {
expect.stringContaining('was not wrapped in act')
)
}

spy.mockRestore()
})

it('should warn one-time-only that `pure` options has been removed', () => {
const spy = jest.spyOn(console, 'error').mockImplementation(() => {})
const store: Store = createStore(stringBuilder)

class ContainerA extends Component {
render() {
return <Passthrough {...this.props} />
}
}

const ConnectedContainerA = connect(
(state) => ({ string: state }),
() => ({}),
() => ({}),
// The `pure` option has been removed
// @ts-ignore
{ pure: true }
)(ContainerA)

class ContainerB extends Component {
render() {
return <Passthrough {...this.props} />
}
}

const ConnectedContainerB = connect(
(state) => ({ string: state }),
() => ({}),
() => ({}),
// The `pure` option has been removed
// @ts-ignore
{ pure: true }
)(ContainerB)

rtl.render(
<ProviderMock store={store}>
<ConnectedContainerA />
<ConnectedContainerB />
</ProviderMock>
)

expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledWith(
'The `pure` option has been removed. `connect` is now always a "pure/memoized" component'
)

spy.mockRestore()
})
})

Expand Down Expand Up @@ -3236,9 +3287,13 @@ describe('React', () => {
</ProviderMock>
)

store.dispatch({ type: '' })
store.dispatch({ type: '' })
outerComponent.current!.setState(({ count }) => ({ count: count + 1 }))
rtl.act(() => {
store.dispatch({ type: '' })
store.dispatch({ type: '' })
outerComponent.current!.setState(({ count }) => ({
count: count + 1,
}))
})

expect(reduxCountPassedToMapState).toEqual(3)
})
Expand Down