Skip to content

Commit 37c6cc7

Browse files
committed
fix[react-devtools/ReactDebugHooks]: support unstable prefixes in hooks and useContextWithBailout
1 parent 7771d3a commit 37c6cc7

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

packages/react-debug-tools/src/ReactDebugHooks.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type {
2020
Dependencies,
2121
Fiber,
2222
Dispatcher as DispatcherType,
23+
ContextDependencyWithSelect,
2324
} from 'react-reconciler/src/ReactInternalTypes';
2425
import type {TransitionStatus} from 'react-reconciler/src/ReactFiberConfig';
2526

@@ -37,7 +38,6 @@ import {
3738
REACT_CONTEXT_TYPE,
3839
} from 'shared/ReactSymbols';
3940
import hasOwnProperty from 'shared/hasOwnProperty';
40-
import type {ContextDependencyWithSelect} from '../../react-reconciler/src/ReactInternalTypes';
4141

4242
type CurrentDispatcherRef = typeof ReactSharedInternals;
4343

@@ -76,7 +76,13 @@ function getPrimitiveStackCache(): Map<string, Array<any>> {
7676
try {
7777
// Use all hooks here to add them to the hook log.
7878
Dispatcher.useContext(({_currentValue: null}: any));
79-
Dispatcher.useState(null);
79+
if (typeof Dispatcher.unstable_useContextWithBailout === 'function') {
80+
// This type check is for Flow only.
81+
Dispatcher.unstable_useContextWithBailout(
82+
({_currentValue: null}: any),
83+
null,
84+
);
85+
}
8086
Dispatcher.useReducer((s: mixed, a: mixed) => s, null);
8187
Dispatcher.useRef(null);
8288
if (typeof Dispatcher.useCacheRefresh === 'function') {
@@ -280,6 +286,22 @@ function useContext<T>(context: ReactContext<T>): T {
280286
return value;
281287
}
282288

289+
function unstable_useContextWithBailout<T>(
290+
context: ReactContext<T>,
291+
select: (T => Array<mixed>) | null,
292+
): T {
293+
const value = readContext(context);
294+
hookLog.push({
295+
displayName: context.displayName || null,
296+
primitive: 'ContextWithBailout',
297+
stackError: new Error(),
298+
value: value,
299+
debugInfo: null,
300+
dispatcherHookName: 'ContextWithBailout',
301+
});
302+
return value;
303+
}
304+
283305
function useState<S>(
284306
initialState: (() => S) | S,
285307
): [S, Dispatch<BasicStateAction<S>>] {
@@ -753,6 +775,7 @@ const Dispatcher: DispatcherType = {
753775
useCacheRefresh,
754776
useCallback,
755777
useContext,
778+
unstable_useContextWithBailout,
756779
useEffect,
757780
useImperativeHandle,
758781
useDebugValue,
@@ -954,6 +977,11 @@ function parseHookName(functionName: void | string): string {
954977
} else {
955978
startIndex += 1;
956979
}
980+
981+
if (functionName.slice(startIndex).startsWith('unstable_')) {
982+
startIndex += 'unstable_'.length;
983+
}
984+
957985
if (functionName.slice(startIndex, startIndex + 3) === 'use') {
958986
if (functionName.length - startIndex === 3) {
959987
return 'Use';

0 commit comments

Comments
 (0)