Skip to content

Commit a1ea294

Browse files
committed
Remove pure option from connect
1 parent 73785e0 commit a1ea294

File tree

5 files changed

+20
-301
lines changed

5 files changed

+20
-301
lines changed

src/components/connect.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ export interface ConnectOptions<
236236
> {
237237
forwardRef?: boolean
238238
context?: typeof ReactReduxContext
239-
pure?: boolean
240239
areStatesEqual?: (nextState: State, prevState: State) => boolean
241240

242241
areOwnPropsEqual?: (
@@ -463,7 +462,9 @@ function connect<
463462
mapDispatchToProps?: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
464463
mergeProps?: MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps>,
465464
{
466-
pure = true,
465+
// The `pure` option has been removed, so TS doesn't like us destructuring this to check its existence.
466+
// @ts-ignore
467+
pure,
467468
areStatesEqual = strictEqual,
468469
areOwnPropsEqual = shallowEqual,
469470
areStatePropsEqual = shallowEqual,
@@ -476,6 +477,14 @@ function connect<
476477
context = ReactReduxContext,
477478
}: ConnectOptions<unknown, unknown, unknown, unknown> = {}
478479
): unknown {
480+
if (process.env.NODE_ENV !== 'production') {
481+
if (pure !== undefined) {
482+
throw new Error(
483+
'The `pure` option has been removed. `connect` is now always a "pure/memoized" component'
484+
)
485+
}
486+
}
487+
479488
const Context = context
480489

481490
type WrappedComponentProps = TOwnProps & ConnectProps
@@ -773,13 +782,14 @@ function connect<
773782
}
774783

775784
// If we're in "pure" mode, ensure our wrapper component only re-renders when incoming props have changed.
776-
const _Connect = pure ? React.memo(ConnectFunction) : ConnectFunction
785+
const _Connect = React.memo(ConnectFunction)
777786

778787
type ConnectedWrapperComponent = typeof _Connect & {
779788
WrappedComponent: typeof WrappedComponent
780789
}
781790

782-
const Connect = _Connect as ConnectedComponent<
791+
// Add a hacky cast to get the right output type
792+
const Connect = _Connect as unknown as ConnectedComponent<
783793
typeof WrappedComponent,
784794
WrappedComponentProps
785795
>

src/connect/mergeProps.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export function defaultMergeProps<TStateProps, TDispatchProps, TOwnProps>(
1717

1818
interface InitMergeOptions {
1919
displayName: string
20-
pure?: boolean
2120
areMergedPropsEqual: (a: any, b: any) => boolean
2221
}
2322

@@ -34,7 +33,7 @@ export function wrapMergePropsFunc<
3433
) => MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps> {
3534
return function initMergePropsProxy(
3635
dispatch,
37-
{ displayName, pure, areMergedPropsEqual }
36+
{ displayName, areMergedPropsEqual }
3837
) {
3938
let hasRunOnce = false
4039
let mergedProps: TMergedProps
@@ -47,7 +46,7 @@ export function wrapMergePropsFunc<
4746
const nextMergedProps = mergeProps(stateProps, dispatchProps, ownProps)
4847

4948
if (hasRunOnce) {
50-
if (!pure || !areMergedPropsEqual(nextMergedProps, mergedProps))
49+
if (!areMergedPropsEqual(nextMergedProps, mergedProps))
5150
mergedProps = nextMergedProps
5251
} else {
5352
hasRunOnce = true

src/connect/selectorFactory.ts

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -66,29 +66,6 @@ export type MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps> = (
6666
ownProps: TOwnProps
6767
) => TMergedProps
6868

69-
export function impureFinalPropsSelectorFactory<
70-
TStateProps,
71-
TOwnProps,
72-
TDispatchProps,
73-
TMergedProps,
74-
State = DefaultRootState
75-
>(
76-
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
77-
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
78-
mergeProps: MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps>,
79-
dispatch: Dispatch
80-
) {
81-
return function impureFinalPropsSelector(state: State, ownProps: TOwnProps) {
82-
return mergeProps(
83-
// @ts-ignore
84-
mapStateToProps(state, ownProps),
85-
// @ts-ignore
86-
mapDispatchToProps(dispatch, ownProps),
87-
ownProps
88-
)
89-
}
90-
}
91-
9269
interface PureSelectorFactoryComparisonOptions<
9370
TOwnProps,
9471
State = DefaultRootState
@@ -97,7 +74,6 @@ interface PureSelectorFactoryComparisonOptions<
9774
areOwnPropsEqual: EqualityFn<TOwnProps>
9875
areStatePropsEqual: EqualityFn<unknown>
9976
displayName: string
100-
pure?: boolean
10177
}
10278

10379
export function pureFinalPropsSelectorFactory<
@@ -222,10 +198,9 @@ export interface SelectorFactoryOptions<
222198

223199
// TODO: Add more comments
224200

225-
// If pure is true, the selector returned by selectorFactory will memoize its results,
201+
// The selector returned by selectorFactory will memoize its results,
226202
// allowing connect's shouldComponentUpdate to return false if final
227-
// props have not changed. If false, the selector will always return a new
228-
// object and shouldComponentUpdate will always return true.
203+
// props have not changed.
229204

230205
export default function finalPropsSelectorFactory<
231206
TStateProps,
@@ -256,9 +231,7 @@ export default function finalPropsSelectorFactory<
256231
verifySubselectors(mapStateToProps, mapDispatchToProps, mergeProps)
257232
}
258233

259-
const selectorFactory = options.pure
260-
? pureFinalPropsSelectorFactory
261-
: impureFinalPropsSelectorFactory
234+
const selectorFactory = pureFinalPropsSelectorFactory
262235

263236
return selectorFactory(
264237
// @ts-ignore

0 commit comments

Comments
 (0)