MCPcopy
hub / github.com/facebook/react / inspectHooksOfFiber

Function inspectHooksOfFiber

packages/react-debug-tools/src/ReactDebugHooks.js:1298–1392  ·  view source on GitHub ↗
(
  fiber: Fiber,
  currentDispatcher: ?CurrentDispatcherRef,
)

Source from the content-addressed store, hash-verified

1296}
1297
1298export function inspectHooksOfFiber(
1299 fiber: Fiber,
1300 currentDispatcher: ?CurrentDispatcherRef,
1301): HooksTree {
1302 // DevTools will pass the current renderer's injected dispatcher.
1303 // Other apps might compile debug hooks as part of their app though.
1304 if (currentDispatcher == null) {
1305 currentDispatcher = ReactSharedInternals;
1306 }
1307
1308 if (
1309 fiber.tag !== FunctionComponent &&
1310 fiber.tag !== SimpleMemoComponent &&
1311 fiber.tag !== ForwardRef
1312 ) {
1313 throw new Error(
1314 'Unknown Fiber. Needs to be a function component to inspect hooks.',
1315 );
1316 }
1317
1318 // Warm up the cache so that it doesn't consume the currentHook.
1319 getPrimitiveStackCache();
1320
1321 // Set up the current hook so that we can step through and read the
1322 // current state from them.
1323 currentHook = (fiber.memoizedState: Hook);
1324 currentFiber = fiber;
1325 const thenableState =
1326 fiber.dependencies && fiber.dependencies._debugThenableState;
1327 // In DEV the thenableState is an inner object.
1328 const usedThenables: any = thenableState
1329 ? thenableState.thenables || thenableState
1330 : null;
1331 currentThenableState = Array.isArray(usedThenables) ? usedThenables : null;
1332 currentThenableIndex = 0;
1333
1334 if (hasOwnProperty.call(currentFiber, 'dependencies')) {
1335 // $FlowFixMe[incompatible-use]: Flow thinks hasOwnProperty might have nulled `currentFiber`
1336 const dependencies = currentFiber.dependencies;
1337 currentContextDependency =
1338 dependencies !== null ? dependencies.firstContext : null;
1339 } else if (hasOwnProperty.call(currentFiber, 'dependencies_old')) {
1340 const dependencies: Dependencies = (currentFiber: any).dependencies_old;
1341 currentContextDependency =
1342 dependencies !== null ? dependencies.firstContext : null;
1343 } else if (hasOwnProperty.call(currentFiber, 'dependencies_new')) {
1344 const dependencies: Dependencies = (currentFiber: any).dependencies_new;
1345 currentContextDependency =
1346 dependencies !== null ? dependencies.firstContext : null;
1347 } else if (hasOwnProperty.call(currentFiber, 'contextDependencies')) {
1348 const contextDependencies = (currentFiber: any).contextDependencies;
1349 currentContextDependency =
1350 contextDependencies !== null ? contextDependencies.first : null;
1351 } else {
1352 throw new Error(
1353 'Unsupported React version. This is a bug in React Debug Tools.',
1354 );
1355 }

Callers 1

inspectHooksFunction · 0.90

Calls 6

getPrimitiveStackCacheFunction · 0.85
resolveDefaultPropsFunction · 0.85
setupContextsFunction · 0.85
inspectHooksOfForwardRefFunction · 0.85
restoreContextsFunction · 0.85
inspectHooksFunction · 0.70

Tested by

no test coverage detected