Skip to content

Commit 8726416

Browse files
committed
Fix DevTools
1 parent c35c642 commit 8726416

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

packages/react-devtools-shared/src/backend/ReactSymbols.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ export const PROFILER_SYMBOL_STRING = 'Symbol(react.profiler)';
5151
export const PROVIDER_NUMBER = 0xeacd;
5252
export const PROVIDER_SYMBOL_STRING = 'Symbol(react.provider)';
5353

54+
export const CONSUMER_SYMBOL_STRING = 'Symbol(react.consumer)';
55+
5456
export const SCOPE_NUMBER = 0xead7;
5557
export const SCOPE_SYMBOL_STRING = 'Symbol(react.scope)';
5658

packages/react-devtools-shared/src/backend/renderer.js

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import {
7979
PROVIDER_SYMBOL_STRING,
8080
CONTEXT_NUMBER,
8181
CONTEXT_SYMBOL_STRING,
82+
CONSUMER_SYMBOL_STRING,
8283
STRICT_MODE_NUMBER,
8384
STRICT_MODE_SYMBOL_STRING,
8485
PROFILER_NUMBER,
@@ -525,6 +526,15 @@ export function getInternalReactConstants(version: string): {
525526
case CONTEXT_NUMBER:
526527
case CONTEXT_SYMBOL_STRING:
527528
case SERVER_CONTEXT_SYMBOL_STRING:
529+
if (
530+
fiber.type._context === undefined &&
531+
fiber.type.Provider === fiber.type
532+
) {
533+
// In 19+, Context.Provider === Context, so this is a provider.
534+
resolvedContext = fiber.type;
535+
return `${resolvedContext.displayName || 'Context'}.Provider`;
536+
}
537+
528538
// 16.3-16.5 read from "type" because the Consumer is the actual context object.
529539
// 16.6+ should read from "type._context" because Consumer can be different (in DEV).
530540
// NOTE Keep in sync with inspectElementRaw()
@@ -533,6 +543,10 @@ export function getInternalReactConstants(version: string): {
533543
// NOTE: TraceUpdatesBackendManager depends on the name ending in '.Consumer'
534544
// If you change the name, figure out a more resilient way to detect it.
535545
return `${resolvedContext.displayName || 'Context'}.Consumer`;
546+
case CONSUMER_SYMBOL_STRING:
547+
// 19+
548+
resolvedContext = fiber.type._context;
549+
return `${resolvedContext.displayName || 'Context'}.Consumer`;
536550
case STRICT_MODE_NUMBER:
537551
case STRICT_MODE_SYMBOL_STRING:
538552
return null;
@@ -3178,8 +3192,14 @@ export function attach(
31783192
}
31793193
}
31803194
} else if (
3181-
typeSymbol === CONTEXT_NUMBER ||
3182-
typeSymbol === CONTEXT_SYMBOL_STRING
3195+
// Detect pre-19 Context Consumers
3196+
(typeSymbol === CONTEXT_NUMBER || typeSymbol === CONTEXT_SYMBOL_STRING) &&
3197+
!(
3198+
// In 19+, CONTEXT_SYMBOL_STRING means a Provider instead.
3199+
// It will be handled in a different branch below.
3200+
// Eventually, this entire branch can be removed.
3201+
(type._context === undefined && type.Provider === type)
3202+
)
31833203
) {
31843204
// 16.3-16.5 read from "type" because the Consumer is the actual context object.
31853205
// 16.6+ should read from "type._context" because Consumer can be different (in DEV).
@@ -3209,6 +3229,35 @@ export function attach(
32093229
}
32103230
}
32113231

3232+
current = current.return;
3233+
}
3234+
} else if (
3235+
// Detect 19+ Context Consumers
3236+
typeSymbol === CONSUMER_SYMBOL_STRING
3237+
) {
3238+
// This branch is 19+ only, where Context.Provider === Context.
3239+
// NOTE Keep in sync with getDisplayNameForFiber()
3240+
const consumerResolvedContext = type._context;
3241+
3242+
// Global context value.
3243+
context = consumerResolvedContext._currentValue || null;
3244+
3245+
// Look for overridden value.
3246+
let current = ((fiber: any): Fiber).return;
3247+
while (current !== null) {
3248+
const currentType = current.type;
3249+
const currentTypeSymbol = getTypeSymbol(currentType);
3250+
if (
3251+
// In 19+, these are Context Providers
3252+
currentTypeSymbol === CONTEXT_SYMBOL_STRING
3253+
) {
3254+
const providerResolvedContext = currentType;
3255+
if (providerResolvedContext === consumerResolvedContext) {
3256+
context = current.memoizedProps.value;
3257+
break;
3258+
}
3259+
}
3260+
32123261
current = current.return;
32133262
}
32143263
}

0 commit comments

Comments
 (0)