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

Function unmountComponentAtNode

packages/react-dom/src/client/ReactDOMRootFB.js:435–508  ·  view source on GitHub ↗
(container: Container)

Source from the content-addressed store, hash-verified

433}
434
435export function unmountComponentAtNode(container: Container): boolean {
436 if (disableLegacyMode) {
437 if (__DEV__) {
438 console.error(
439 'unmountComponentAtNode was removed in React 19. Use root.unmount() instead.',
440 );
441 }
442 throw new Error('ReactDOM: Unsupported Legacy Mode API.');
443 }
444 if (!isValidContainer(container)) {
445 throw new Error('Target container is not a DOM element.');
446 }
447
448 if (__DEV__) {
449 const isModernRoot =
450 isContainerMarkedAsRoot(container) &&
451 container._reactRootContainer === undefined;
452 if (isModernRoot) {
453 console.error(
454 'You are calling ReactDOM.unmountComponentAtNode() on a container that was previously ' +
455 'passed to ReactDOMClient.createRoot(). This is not supported. Did you mean to call root.unmount()?',
456 );
457 }
458 }
459
460 if (container._reactRootContainer) {
461 const root = container._reactRootContainer;
462
463 if (__DEV__) {
464 const rootEl = getReactRootElementInContainer(container);
465 const renderedByDifferentReact = rootEl && !getInstanceFromNode(rootEl);
466 if (renderedByDifferentReact) {
467 console.error(
468 "unmountComponentAtNode(): The node you're attempting to unmount " +
469 'was rendered by another copy of React.',
470 );
471 }
472 }
473
474 updateContainerSync(null, root, null, null);
475 flushSyncWork();
476 // $FlowFixMe[incompatible-type] This should probably use `delete container._reactRootContainer`
477 container._reactRootContainer = null;
478 unmarkContainerAsRoot(container);
479 return true;
480 } else {
481 if (__DEV__) {
482 const rootEl = getReactRootElementInContainer(container);
483 const hasNonRootReactChild = !!(rootEl && getInstanceFromNode(rootEl));
484
485 // Check if the container itself is a React root node.
486 const isContainerReactRoot =
487 container.nodeType === ELEMENT_NODE &&
488 isValidContainer(container.parentNode) &&
489 // $FlowFixMe[prop-missing]
490 // $FlowFixMe[incompatible-use]
491 !!container.parentNode._reactRootContainer;
492

Callers 1

mountLegacyAppFunction · 0.50

Calls 8

isValidContainerFunction · 0.90
isContainerMarkedAsRootFunction · 0.90
getInstanceFromNodeFunction · 0.90
updateContainerSyncFunction · 0.90
unmarkContainerAsRootFunction · 0.90
errorMethod · 0.65
flushSyncWorkFunction · 0.50

Tested by

no test coverage detected