Skip to content

Commit 69413ff

Browse files
authored
Merge pull request #1951 from jspurlin/jspurlin/passOwnPropsToASE
2 parents bbc546e + 527eb36 commit 69413ff

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

docs/api/connect.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ connect(mapStateToProps, mapDispatchToProps, null, { context: MyContext })(
233233
)
234234
```
235235
236-
#### `areStatesEqual: (next: Object, prev: Object) => boolean`
236+
#### `areStatesEqual: (next: Object, prev: Object, nextOwnProps: Object, prevOwnProps: Object) => boolean`
237237
238238
- default value: `strictEqual: (next, prev) => prev === next`
239239
@@ -244,7 +244,7 @@ const areStatesEqual = (next, prev) =>
244244
prev.entities.todos === next.entities.todos
245245
```
246246
247-
You may wish to override `areStatesEqual` if your `mapStateToProps` function is computationally expensive and is also only concerned with a small slice of your state. The example above will effectively ignore state changes for everything but that slice of state.
247+
You may wish to override `areStatesEqual` if your `mapStateToProps` function is computationally expensive and is also only concerned with a small slice of your state. The example above will effectively ignore state changes for everything but that slice of state. Additionally, `areStatesEqual` provides `nextOwnProps` and `prevOwnProps` to allow for more effective scoping of your state which your connected component is interested in, if needed.
248248
249249
This would likely impact the other equality checks as well, depending on your `mapStateToProps` function.
250250

src/components/connect.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,12 @@ export interface ConnectOptions<
231231
> {
232232
forwardRef?: boolean
233233
context?: typeof ReactReduxContext
234-
areStatesEqual?: (nextState: State, prevState: State) => boolean
234+
areStatesEqual?: (
235+
nextState: State,
236+
prevState: State,
237+
nextOwnProps: TOwnProps,
238+
prevOwnProps: TOwnProps
239+
) => boolean
235240

236241
areOwnPropsEqual?: (
237242
nextOwnProps: TOwnProps,
@@ -696,7 +701,7 @@ function connect<
696701
notifyNestedSubs,
697702
])
698703

699-
let actualChildProps: uSES
704+
let actualChildProps: Record<string, unknown>
700705

701706
try {
702707
actualChildProps = useSyncExternalStore(

src/connect/selectorFactory.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Dispatch, Action } from 'redux'
22
import type { ComponentType } from 'react'
33
import verifySubselectors from './verifySubselectors'
4-
import type { EqualityFn } from '../types'
4+
import type { EqualityFn, ExtendedEqualityFn } from '../types'
55

66
export type SelectorFactory<S, TProps, TOwnProps, TFactoryOptions> = (
77
dispatch: Dispatch<Action<unknown>>,
@@ -59,7 +59,7 @@ export type MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps> = (
5959
) => TMergedProps
6060

6161
interface PureSelectorFactoryComparisonOptions<TStateProps, TOwnProps, State> {
62-
readonly areStatesEqual: EqualityFn<State>
62+
readonly areStatesEqual: ExtendedEqualityFn<State, TOwnProps>
6363
readonly areStatePropsEqual: EqualityFn<TStateProps>
6464
readonly areOwnPropsEqual: EqualityFn<TOwnProps>
6565
}
@@ -132,7 +132,12 @@ export function pureFinalPropsSelectorFactory<
132132

133133
function handleSubsequentCalls(nextState: State, nextOwnProps: TOwnProps) {
134134
const propsChanged = !areOwnPropsEqual(nextOwnProps, ownProps)
135-
const stateChanged = !areStatesEqual(nextState, state)
135+
const stateChanged = !areStatesEqual(
136+
nextState,
137+
state,
138+
nextOwnProps,
139+
ownProps
140+
)
136141
state = nextState
137142
ownProps = nextOwnProps
138143

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export type FixTypeLater = any
1515

1616
export type EqualityFn<T> = (a: T, b: T) => boolean
1717

18+
export type ExtendedEqualityFn<T, P> = (a: T, b: T, c: P, d: P) => boolean
19+
1820
export type AnyIfEmpty<T extends object> = keyof T extends never ? any : T
1921

2022
export type DistributiveOmit<T, K extends keyof T> = T extends unknown

0 commit comments

Comments
 (0)