Skip to content

Commit

Permalink
Ensure backwards compatibility with older reconciler versions
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Mar 13, 2024
1 parent 0775186 commit 0df1e46
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {
} from 'shared/ReactTypes';
import type {
ContextDependency,
Dependencies,
Fiber,
Dispatcher as DispatcherType,
} from 'react-reconciler/src/ReactInternalTypes';
Expand All @@ -33,6 +34,7 @@ import {
REACT_MEMO_CACHE_SENTINEL,
REACT_CONTEXT_TYPE,
} from 'shared/ReactSymbols';
import hasOwnProperty from 'shared/hasOwnProperty';

type CurrentDispatcherRef = typeof ReactSharedInternals.ReactCurrentDispatcher;

Expand Down Expand Up @@ -157,7 +159,7 @@ function readContext<T>(context: ReactContext<T>): T {
} else {
if (currentContextDependency === null) {
throw new Error(
'Context reads do not line up with context dependencies. This is a bug in React.',
'Context reads do not line up with context dependencies. This is a bug in React Debug Tools.',
);
}

Expand Down Expand Up @@ -1075,9 +1077,28 @@ export function inspectHooksOfFiber(
// current state from them.
currentHook = (fiber.memoizedState: Hook);
currentFiber = fiber;
const dependencies = currentFiber.dependencies;
currentContextDependency =
dependencies !== null ? dependencies.firstContext : null;
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;
Expand Down

0 comments on commit 0df1e46

Please sign in to comment.