MCPcopy Index your code
hub / github.com/react/react / getIDForHostInstance

Method getIDForHostInstance

packages/react-devtools-shared/src/backend/agent.js:338–394  ·  view source on GitHub ↗
(target: HostInstance)

Source from the content-addressed store, hash-verified

336 }
337
338 getIDForHostInstance(target: HostInstance): number | null {
339 if (isReactNativeEnvironment() || typeof target.nodeType !== 'number') {
340 // In React Native or non-DOM we simply pick any renderer that has a match.
341 for (const rendererID in this._rendererInterfaces) {
342 const renderer = ((this._rendererInterfaces[
343 (rendererID: any)
344 ]: any): RendererInterface);
345 try {
346 const match = renderer.getElementIDForHostInstance(target);
347 if (match != null) {
348 return match;
349 }
350 } catch (error) {
351 // Some old React versions might throw if they can't find a match.
352 // If so we should ignore it...
353 }
354 }
355 return null;
356 } else {
357 // In the DOM we use a smarter mechanism to find the deepest a DOM node
358 // that is registered if there isn't an exact match.
359 let bestMatch: null | Element = null;
360 let bestRenderer: null | RendererInterface = null;
361 // Find the nearest ancestor which is mounted by a React.
362 for (const rendererID in this._rendererInterfaces) {
363 const renderer = ((this._rendererInterfaces[
364 (rendererID: any)
365 ]: any): RendererInterface);
366 const nearestNode: null | Element = renderer.getNearestMountedDOMNode(
367 (target: any),
368 );
369 if (nearestNode !== null) {
370 if (nearestNode === target) {
371 // Exact match we can exit early.
372 bestMatch = nearestNode;
373 bestRenderer = renderer;
374 break;
375 }
376 if (bestMatch === null || bestMatch.contains(nearestNode)) {
377 // If this is the first match or the previous match contains the new match,
378 // so the new match is a deeper and therefore better match.
379 bestMatch = nearestNode;
380 bestRenderer = renderer;
381 }
382 }
383 }
384 if (bestRenderer != null && bestMatch != null) {
385 try {
386 return bestRenderer.getElementIDForHostInstance(bestMatch);
387 } catch (error) {
388 // Some old React versions might throw if they can't find a match.
389 // If so we should ignore it...
390 }
391 }
392 return null;
393 }
394 }
395

Callers 3

selectNodeMethod · 0.95
selectElementForNodeFunction · 0.80
store-test.jsFile · 0.80

Calls 2

isReactNativeEnvironmentFunction · 0.90
containsMethod · 0.65

Tested by

no test coverage detected