@@ -18,10 +18,49 @@ import {
1818 */
1919export type DevModeCheckFrequency = 'never' | 'once' | 'always'
2020
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+
2153export interface UseSelectorOptions < Selected = unknown > {
2254 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 >
2564}
2665
2766export interface UseSelector {
@@ -65,13 +104,10 @@ export function createSelectorHook(
65104 | EqualityFn < NoInfer < Selected > >
66105 | UseSelectorOptions < NoInfer < Selected > > = { }
67106 ) : 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
75111 if ( process . env . NODE_ENV !== 'production' ) {
76112 if ( ! selector ) {
77113 throw new Error ( `You must pass a selector to useSelector` )
@@ -90,8 +126,8 @@ export function createSelectorHook(
90126 store,
91127 subscription,
92128 getServerState,
93- stabilityCheck : globalStabilityCheck ,
94- identityFunctionCheck : globalIdentityFunctionCheck ,
129+ stabilityCheck,
130+ identityFunctionCheck,
95131 } = useReduxContext ( )
96132
97133 const firstRun = React . useRef ( true )
@@ -101,10 +137,14 @@ export function createSelectorHook(
101137 [ selector . name ] ( state : TState ) {
102138 const selected = selector ( state )
103139 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+ }
108148 if (
109149 finalStabilityCheck === 'always' ||
110150 ( finalStabilityCheck === 'once' && firstRun . current )
@@ -131,10 +171,6 @@ export function createSelectorHook(
131171 )
132172 }
133173 }
134- const finalIdentityFunctionCheck =
135- typeof identityFunctionCheck === 'undefined'
136- ? globalIdentityFunctionCheck
137- : identityFunctionCheck
138174 if (
139175 finalIdentityFunctionCheck === 'always' ||
140176 ( finalIdentityFunctionCheck === 'once' && firstRun . current )
@@ -161,7 +197,7 @@ export function createSelectorHook(
161197 return selected
162198 } ,
163199 } [ selector . name ] ,
164- [ selector , globalStabilityCheck , stabilityCheck ]
200+ [ selector , stabilityCheck , devModeChecks . stabilityCheck ]
165201 )
166202
167203 const selectedState = useSyncExternalStoreWithSelector (
0 commit comments