@@ -18,10 +18,49 @@ import {
18
18
*/
19
19
export type DevModeCheckFrequency = 'never' | 'once' | 'always'
20
20
21
+ /**
22
+ * Represents the configuration for development mode checks.
23
+ *
24
+ * @since 9.0.0
25
+ * @internal
26
+ */
27
+ export interface DevModeChecks {
28
+ /**
29
+ * Overrides the global stability check for the selector.
30
+ * - `once` - Run only the first time the selector is called.
31
+ * - `always` - Run every time the selector is called.
32
+ * - `never` - Never run the stability check.
33
+ *
34
+ * @default 'once'
35
+ *
36
+ * @since 8.1.0
37
+ */
38
+ stabilityCheck : DevModeCheckFrequency
39
+
40
+ /**
41
+ * Overrides the global identity function check for the selector.
42
+ * - `once` - Run only the first time the selector is called.
43
+ * - `always` - Run every time the selector is called.
44
+ * - `never` - Never run the identity function check.
45
+ *
46
+ * @default 'once'
47
+ *
48
+ * @since 9.0.0
49
+ */
50
+ identityFunctionCheck : DevModeCheckFrequency
51
+ }
52
+
21
53
export interface UseSelectorOptions < Selected = unknown > {
22
54
equalityFn ?: EqualityFn < Selected >
23
- stabilityCheck ?: DevModeCheckFrequency
24
- identityFunctionCheck ?: DevModeCheckFrequency
55
+
56
+ /**
57
+ * `useSelector` performs additional checks in development mode to help
58
+ * identify and warn about potential issues in selector behavior. This
59
+ * option allows you to customize the behavior of these checks per selector.
60
+ *
61
+ * @since 9.0.0
62
+ */
63
+ devModeChecks ?: Partial < DevModeChecks >
25
64
}
26
65
27
66
export interface UseSelector {
@@ -65,13 +104,10 @@ export function createSelectorHook(
65
104
| EqualityFn < NoInfer < Selected > >
66
105
| UseSelectorOptions < NoInfer < Selected > > = { }
67
106
) : Selected {
68
- const {
69
- equalityFn = refEquality ,
70
- stabilityCheck = undefined ,
71
- identityFunctionCheck = undefined ,
72
- } = typeof equalityFnOrOptions === 'function'
73
- ? { equalityFn : equalityFnOrOptions }
74
- : equalityFnOrOptions
107
+ const { equalityFn = refEquality , devModeChecks = { } } =
108
+ typeof equalityFnOrOptions === 'function'
109
+ ? { equalityFn : equalityFnOrOptions }
110
+ : equalityFnOrOptions
75
111
if ( process . env . NODE_ENV !== 'production' ) {
76
112
if ( ! selector ) {
77
113
throw new Error ( `You must pass a selector to useSelector` )
@@ -90,8 +126,8 @@ export function createSelectorHook(
90
126
store,
91
127
subscription,
92
128
getServerState,
93
- stabilityCheck : globalStabilityCheck ,
94
- identityFunctionCheck : globalIdentityFunctionCheck ,
129
+ stabilityCheck,
130
+ identityFunctionCheck,
95
131
} = useReduxContext ( )
96
132
97
133
const firstRun = React . useRef ( true )
@@ -101,10 +137,14 @@ export function createSelectorHook(
101
137
[ selector . name ] ( state : TState ) {
102
138
const selected = selector ( state )
103
139
if ( process . env . NODE_ENV !== 'production' ) {
104
- const finalStabilityCheck =
105
- typeof stabilityCheck === 'undefined'
106
- ? globalStabilityCheck
107
- : stabilityCheck
140
+ const {
141
+ identityFunctionCheck : finalIdentityFunctionCheck ,
142
+ stabilityCheck : finalStabilityCheck ,
143
+ } = {
144
+ stabilityCheck,
145
+ identityFunctionCheck,
146
+ ...devModeChecks ,
147
+ }
108
148
if (
109
149
finalStabilityCheck === 'always' ||
110
150
( finalStabilityCheck === 'once' && firstRun . current )
@@ -131,10 +171,6 @@ export function createSelectorHook(
131
171
)
132
172
}
133
173
}
134
- const finalIdentityFunctionCheck =
135
- typeof identityFunctionCheck === 'undefined'
136
- ? globalIdentityFunctionCheck
137
- : identityFunctionCheck
138
174
if (
139
175
finalIdentityFunctionCheck === 'always' ||
140
176
( finalIdentityFunctionCheck === 'once' && firstRun . current )
@@ -161,7 +197,7 @@ export function createSelectorHook(
161
197
return selected
162
198
} ,
163
199
} [ selector . name ] ,
164
- [ selector , globalStabilityCheck , stabilityCheck ]
200
+ [ selector , stabilityCheck , devModeChecks . stabilityCheck ]
165
201
)
166
202
167
203
const selectedState = useSyncExternalStoreWithSelector (
0 commit comments