Skip to content

Commit

Permalink
Not all versions of fiber.dependencies have `contextDependency.memo…
Browse files Browse the repository at this point in the history
…izedValue`

I considered polyfilling `memoizedValue` but it seems safer to call this out when we read the context value.
And also to avoid deopts due to using expando properties on the ContextDependency type in old versions.
  • Loading branch information
eps1lon committed Mar 13, 2024
1 parent 4c2a435 commit 088fb2c
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,29 @@ export function inspectHooksOfFiber(
currentHook = (fiber.memoizedState: Hook);
currentFiber = fiber;

if (hasOwnProperty.call(currentFiber, 'dependencies')) {
// $FlowFixMe[incompatible-use]: Flow thinks hasOwnProperty might have nulled `currentFiber`
const dependencies = currentFiber.dependencies;
currentContextDependency =
dependencies !== null ? dependencies.firstContext : null;
} else if (hasOwnProperty.call(currentFiber, 'dependencies_old')) {
const dependencies: Dependencies = (currentFiber: any).dependencies_old;
currentContextDependency =
dependencies !== null ? dependencies.firstContext : null;
} else if (hasOwnProperty.call(currentFiber, 'dependencies_new')) {
const dependencies: Dependencies = (currentFiber: any).dependencies_new;
currentContextDependency =
dependencies !== null ? dependencies.firstContext : null;
} else if (hasOwnProperty.call(currentFiber, 'contextDependencies')) {
const contextDependencies = (currentFiber: any).contextDependencies;
currentContextDependency =
contextDependencies !== null ? contextDependencies.first : null;
} else {
throw new Error(
'Unsupported React version. This is a bug in React Debug Tools.',
);
}

const type = fiber.type;
let props = fiber.memoizedProps;
if (type !== fiber.elementType) {
Expand All @@ -1118,30 +1141,11 @@ export function inspectHooksOfFiber(
// Only used for versions of React without memoized context value in context dependencies.
const contextMap = new Map<ReactContext<any>, any>();
try {
if (hasOwnProperty.call(currentFiber, 'dependencies')) {
// $FlowFixMe[incompatible-use]: Flow thinks hasOwnProperty might have nulled `currentFiber`
const dependencies = currentFiber.dependencies;
currentContextDependency =
dependencies !== null ? dependencies.firstContext : null;
} else if (hasOwnProperty.call(currentFiber, 'dependencies_old')) {
const dependencies: Dependencies = (currentFiber: any).dependencies_old;
currentContextDependency =
dependencies !== null ? dependencies.firstContext : null;
setupContexts(contextMap, fiber);
} else if (hasOwnProperty.call(currentFiber, 'dependencies_new')) {
const dependencies: Dependencies = (currentFiber: any).dependencies_new;
currentContextDependency =
dependencies !== null ? dependencies.firstContext : null;
setupContexts(contextMap, fiber);
} else if (hasOwnProperty.call(currentFiber, 'contextDependencies')) {
const contextDependencies = (currentFiber: any).contextDependencies;
currentContextDependency =
contextDependencies !== null ? contextDependencies.first : null;
if (
currentContextDependency !== null &&
!hasOwnProperty.call(currentContextDependency, 'memoizedValue')
) {
setupContexts(contextMap, fiber);
} else {
throw new Error(
'Unsupported React version. This is a bug in React Debug Tools.',
);
}

if (fiber.tag === ForwardRef) {
Expand Down

0 comments on commit 088fb2c

Please sign in to comment.