Skip to content

Commit 6e219ee

Browse files
committed
show warning instead of throwing error that pure option has been removed
1 parent 8e39fa6 commit 6e219ee

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

src/components/connect.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { mergePropsFactory } from '../connect/mergeProps'
2828
import { createSubscription, Subscription } from '../utils/Subscription'
2929
import { useIsomorphicLayoutEffect } from '../utils/useIsomorphicLayoutEffect'
3030
import shallowEqual from '../utils/shallowEqual'
31+
import warning from '../utils/warning'
3132

3233
import {
3334
ReactReduxContext,
@@ -405,6 +406,8 @@ export interface Connect<DefaultState = unknown> {
405406
// tslint:enable:no-unnecessary-generics
406407
}
407408

409+
let hasWarnedAboutDeprecatedPureOption = false
410+
408411
/**
409412
* Connects a React component to a Redux store.
410413
*
@@ -452,8 +455,9 @@ function connect<
452455
}: ConnectOptions<unknown, unknown, unknown, unknown> = {}
453456
): unknown {
454457
if (process.env.NODE_ENV !== 'production') {
455-
if (pure !== undefined) {
456-
throw new Error(
458+
if (pure !== undefined && !hasWarnedAboutDeprecatedPureOption) {
459+
hasWarnedAboutDeprecatedPureOption = true
460+
warning(
457461
'The `pure` option has been removed. `connect` is now always a "pure/memoized" component'
458462
)
459463
}

test/components/connect.spec.tsx

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2912,6 +2912,57 @@ describe('React', () => {
29122912
expect.stringContaining('was not wrapped in act')
29132913
)
29142914
}
2915+
2916+
spy.mockRestore()
2917+
})
2918+
2919+
it('should warn one-time-only that `pure` options has been removed', () => {
2920+
const spy = jest.spyOn(console, 'error').mockImplementation(() => {})
2921+
const store: Store = createStore(stringBuilder)
2922+
2923+
class ContainerA extends Component {
2924+
render() {
2925+
return <Passthrough {...this.props} />
2926+
}
2927+
}
2928+
2929+
const ConnectedContainerA = connect(
2930+
(state) => ({ string: state }),
2931+
() => ({}),
2932+
() => ({}),
2933+
// The `pure` option has been removed
2934+
// @ts-ignore
2935+
{ pure: true }
2936+
)(ContainerA)
2937+
2938+
class ContainerB extends Component {
2939+
render() {
2940+
return <Passthrough {...this.props} />
2941+
}
2942+
}
2943+
2944+
const ConnectedContainerB = connect(
2945+
(state) => ({ string: state }),
2946+
() => ({}),
2947+
() => ({}),
2948+
// The `pure` option has been removed
2949+
// @ts-ignore
2950+
{ pure: true }
2951+
)(ContainerB)
2952+
2953+
rtl.render(
2954+
<ProviderMock store={store}>
2955+
<ConnectedContainerA />
2956+
<ConnectedContainerB />
2957+
</ProviderMock>
2958+
)
2959+
2960+
expect(spy).toHaveBeenCalledTimes(1)
2961+
expect(spy).toHaveBeenCalledWith(
2962+
'The `pure` option has been removed. `connect` is now always a "pure/memoized" component'
2963+
)
2964+
2965+
spy.mockRestore()
29152966
})
29162967
})
29172968

@@ -3236,9 +3287,13 @@ describe('React', () => {
32363287
</ProviderMock>
32373288
)
32383289

3239-
store.dispatch({ type: '' })
3240-
store.dispatch({ type: '' })
3241-
outerComponent.current!.setState(({ count }) => ({ count: count + 1 }))
3290+
rtl.act(() => {
3291+
store.dispatch({ type: '' })
3292+
store.dispatch({ type: '' })
3293+
outerComponent.current!.setState(({ count }) => ({
3294+
count: count + 1,
3295+
}))
3296+
})
32423297

32433298
expect(reduxCountPassedToMapState).toEqual(3)
32443299
})

0 commit comments

Comments
 (0)