Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Not all versions of fiber.dependencies have `contextDependency.memo…
…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
commit 088fb2c84b1e39d3eb40f84c0de2015f880cda55
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