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

Method getElementAtIndex

packages/react-devtools-shared/src/devtools/store.js:530–605  ·  view source on GitHub ↗
(index: number)

Source from the content-addressed store, hash-verified

528 }
529
530 getElementAtIndex(index: number): Element | null {
531 if (index < 0 || index >= this.numElements) {
532 console.warn(
533 `Invalid index ${index} specified; store contains ${this.numElements} items.`,
534 );
535
536 return null;
537 }
538
539 // Find which root this element is in...
540 let root;
541 let rootWeight = 0;
542 for (let i = 0; i < this._roots.length; i++) {
543 const rootID = this._roots[i];
544 root = this._idToElement.get(rootID);
545
546 if (root === undefined) {
547 this._throwAndEmitError(
548 Error(
549 `Couldn't find root with id "${rootID}": no matching node was found in the Store.`,
550 ),
551 );
552
553 return null;
554 }
555
556 if (root.children.length === 0) {
557 continue;
558 }
559
560 if (rootWeight + root.weight > index) {
561 break;
562 } else {
563 rootWeight += root.weight;
564 }
565 }
566
567 if (root === undefined) {
568 return null;
569 }
570
571 // Find the element in the tree using the weight of each node...
572 // Skip over the root itself, because roots aren't visible in the Elements tree.
573 let currentElement: Element = root;
574 let currentWeight = rootWeight - 1;
575
576 while (index !== currentWeight) {
577 const numChildren = currentElement.children.length;
578 for (let i = 0; i < numChildren; i++) {
579 const childID = currentElement.children[i];
580 const child = this._idToElement.get(childID);
581
582 if (child === undefined) {
583 this._throwAndEmitError(
584 Error(
585 `Couldn't child element with id "${childID}": no matching node was found in the Store.`,
586 ),
587 );

Callers 9

getElementIDAtIndexMethod · 0.95
store-test.jsFile · 0.80
printStoreFunction · 0.80
Tree.jsFile · 0.80
Element.jsFile · 0.80
reduceTreeStateFunction · 0.80

Calls 2

_throwAndEmitErrorMethod · 0.95
getMethod · 0.65

Tested by

no test coverage detected