Skip to content

Commit 088fb2c

Browse files
committed
Not all versions of fiber.dependencies have contextDependency.memoizedValue
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.
1 parent 4c2a435 commit 088fb2c

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

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

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,29 @@ export function inspectHooksOfFiber(
11091109
currentHook = (fiber.memoizedState: Hook);
11101110
currentFiber = fiber;
11111111

1112+
if (hasOwnProperty.call(currentFiber, 'dependencies')) {
1113+
// $FlowFixMe[incompatible-use]: Flow thinks hasOwnProperty might have nulled `currentFiber`
1114+
const dependencies = currentFiber.dependencies;
1115+
currentContextDependency =
1116+
dependencies !== null ? dependencies.firstContext : null;
1117+
} else if (hasOwnProperty.call(currentFiber, 'dependencies_old')) {
1118+
const dependencies: Dependencies = (currentFiber: any).dependencies_old;
1119+
currentContextDependency =
1120+
dependencies !== null ? dependencies.firstContext : null;
1121+
} else if (hasOwnProperty.call(currentFiber, 'dependencies_new')) {
1122+
const dependencies: Dependencies = (currentFiber: any).dependencies_new;
1123+
currentContextDependency =
1124+
dependencies !== null ? dependencies.firstContext : null;
1125+
} else if (hasOwnProperty.call(currentFiber, 'contextDependencies')) {
1126+
const contextDependencies = (currentFiber: any).contextDependencies;
1127+
currentContextDependency =
1128+
contextDependencies !== null ? contextDependencies.first : null;
1129+
} else {
1130+
throw new Error(
1131+
'Unsupported React version. This is a bug in React Debug Tools.',
1132+
);
1133+
}
1134+
11121135
const type = fiber.type;
11131136
let props = fiber.memoizedProps;
11141137
if (type !== fiber.elementType) {
@@ -1118,30 +1141,11 @@ export function inspectHooksOfFiber(
11181141
// Only used for versions of React without memoized context value in context dependencies.
11191142
const contextMap = new Map<ReactContext<any>, any>();
11201143
try {
1121-
if (hasOwnProperty.call(currentFiber, 'dependencies')) {
1122-
// $FlowFixMe[incompatible-use]: Flow thinks hasOwnProperty might have nulled `currentFiber`
1123-
const dependencies = currentFiber.dependencies;
1124-
currentContextDependency =
1125-
dependencies !== null ? dependencies.firstContext : null;
1126-
} else if (hasOwnProperty.call(currentFiber, 'dependencies_old')) {
1127-
const dependencies: Dependencies = (currentFiber: any).dependencies_old;
1128-
currentContextDependency =
1129-
dependencies !== null ? dependencies.firstContext : null;
1130-
setupContexts(contextMap, fiber);
1131-
} else if (hasOwnProperty.call(currentFiber, 'dependencies_new')) {
1132-
const dependencies: Dependencies = (currentFiber: any).dependencies_new;
1133-
currentContextDependency =
1134-
dependencies !== null ? dependencies.firstContext : null;
1135-
setupContexts(contextMap, fiber);
1136-
} else if (hasOwnProperty.call(currentFiber, 'contextDependencies')) {
1137-
const contextDependencies = (currentFiber: any).contextDependencies;
1138-
currentContextDependency =
1139-
contextDependencies !== null ? contextDependencies.first : null;
1144+
if (
1145+
currentContextDependency !== null &&
1146+
!hasOwnProperty.call(currentContextDependency, 'memoizedValue')
1147+
) {
11401148
setupContexts(contextMap, fiber);
1141-
} else {
1142-
throw new Error(
1143-
'Unsupported React version. This is a bug in React Debug Tools.',
1144-
);
11451149
}
11461150

11471151
if (fiber.tag === ForwardRef) {

0 commit comments

Comments
 (0)