facebook/react · error

362

362

Error message

Could not find React container within specified host subtree.

What it means

In findFiberRootForHostRoot, when getInstanceFromNode finds no React instance on the host root, React falls back to findFiberRoot to locate a container in the node's subtree. A null result means the node is neither a React container nor contains one (error code 362), so React cannot reflect from that node into the fiber tree at all.

Source

Thrown at packages/react-reconciler/src/ReactTestSelectors.js:135

  };
}

function findFiberRootForHostRoot(hostRoot: Instance): Fiber {
  const maybeFiber = getInstanceFromNode(hostRoot as any);
  if (maybeFiber != null) {
    if (typeof maybeFiber.memoizedProps['data-testname'] !== 'string') {
      throw new Error(
        'Invalid host root specified. Should be either a React container or a node with a testname attribute.',
      );
    }

    return maybeFiber as any as Fiber;
  } else {
    const fiberRoot = findFiberRoot(hostRoot);

    // $FlowFixMe[invalid-compare]
    if (fiberRoot === null) {
      throw new Error(
        'Could not find React container within specified host subtree.',
      );
    }

    // The Flow type for FiberRoot is a little funky.
    // createFiberRoot() cheats this by treating the root as :any and adding stateNode lazily.
    return (fiberRoot as any).stateNode.current as Fiber;
  }
}

function matchSelector(fiber: Fiber, selector: Selector): boolean {
  const tag = fiber.tag;
  switch (selector.$$typeof) {
    case COMPONENT_TYPE:
      if (fiber.type === selector.value) {
        return true;
      }
      break;

View on GitHub (pinned to eafeac097b)

Solutions

  1. Call the test-selector API only after createRoot(container).render(...) has mounted (wrap tests in act/await)
  2. Pass the exact element you handed to createRoot, from the same document React rendered into
  3. Check the element is connected to the document (isConnected) before querying

Example fix

// before
const container = document.createElement('div'); // never rendered into
findAllNodes(container, [createTestNameSelector('feed')]); // throws: no container in subtree

// after
const container = document.createElement('div');
document.body.appendChild(container);
createRoot(container).render(<App />);
await act(() => {}); // let React mount
findAllNodes(container, [createTestNameSelector('feed')]);
Defensive patterns

Strategy: validation

Validate before calling

const isReactContainer = (el) =>
  el instanceof Element &&
  Object.keys(el).some((k) => k.startsWith('__reactContainer') || k.startsWith('_reactRootContainer'));
// query only after React owns the container
if (isReactContainer(containerEl)) {
  findAllNodes(containerEl, selectors);
}

Type guard

function isReactContainer(el) {
  return el instanceof Element &&
    Object.keys(el).some((k) => k.startsWith('__reactContainer'));
}

Prevention

When it happens

Trigger: Calling test-selector APIs such as findAllNodes on a DOM node that React never rendered into — a wrong node, a different document/iframe/shadow root, or a container that was never mounted.

Common situations: Querying before createRoot(container).render() has mounted; passing an element from another iframe or jsdom window than the one React rendered into; querying after unmount severed the container link; typos in test selector roots.

Related errors


AI-assisted analysis of facebook/react@eafeac097b (2026-08-21). Data as JSON: /api/errors/57b75acaebd09cb4. Report an issue: GitHub.