Home / Function/ validateDOMNesting() — react Function Reference

validateDOMNesting() — react Function Reference

Architecture documentation for the validateDOMNesting() function in validateDOMNesting.js from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  96e5c4d9_bcbf_86ea_69bb_cc3d02fbd794["validateDOMNesting()"]
  99b6f05f_c8d3_afa4_842b_34ebebb1f8a7["validateDOMNesting.js"]
  96e5c4d9_bcbf_86ea_69bb_cc3d02fbd794 -->|defined in| 99b6f05f_c8d3_afa4_842b_34ebebb1f8a7
  af0fc495_4d30_0fb4_1034_e516045de3b8["isTagValidWithParent()"]
  96e5c4d9_bcbf_86ea_69bb_cc3d02fbd794 -->|calls| af0fc495_4d30_0fb4_1034_e516045de3b8
  3f347104_9d92_b69f_afb5_0f4b1003a5b4["findInvalidAncestorForTag()"]
  96e5c4d9_bcbf_86ea_69bb_cc3d02fbd794 -->|calls| 3f347104_9d92_b69f_afb5_0f4b1003a5b4
  efcb6d17_b35d_e8c2_bc5c_34ef4aeaa0a1["findAncestor()"]
  96e5c4d9_bcbf_86ea_69bb_cc3d02fbd794 -->|calls| efcb6d17_b35d_e8c2_bc5c_34ef4aeaa0a1
  330a083b_035c_adf8_6bc7_857221a6c608["describeAncestors()"]
  96e5c4d9_bcbf_86ea_69bb_cc3d02fbd794 -->|calls| 330a083b_035c_adf8_6bc7_857221a6c608
  style 96e5c4d9_bcbf_86ea_69bb_cc3d02fbd794 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/react-dom-bindings/src/client/validateDOMNesting.js lines 549–641

function validateDOMNesting(
  childTag: string,
  ancestorInfo: AncestorInfoDev,
): boolean {
  if (__DEV__) {
    ancestorInfo = ancestorInfo || emptyAncestorInfoDev;
    const parentInfo = ancestorInfo.current;
    const parentTag = parentInfo && parentInfo.tag;

    const invalidParent = isTagValidWithParent(
      childTag,
      parentTag,
      ancestorInfo.implicitRootScope,
    )
      ? null
      : parentInfo;
    const invalidAncestor = invalidParent
      ? null
      : findInvalidAncestorForTag(childTag, ancestorInfo);
    const invalidParentOrAncestor = invalidParent || invalidAncestor;
    if (!invalidParentOrAncestor) {
      return true;
    }

    const ancestorTag = invalidParentOrAncestor.tag;

    const warnKey =
      // eslint-disable-next-line react-internal/safe-string-coercion
      String(!!invalidParent) + '|' + childTag + '|' + ancestorTag;
    if (didWarn[warnKey]) {
      return false;
    }
    didWarn[warnKey] = true;

    const child = current;
    const ancestor = child ? findAncestor(child.return, ancestorTag) : null;

    const ancestorDescription =
      child !== null && ancestor !== null
        ? describeAncestors(ancestor, child, null)
        : '';

    const tagDisplayName = '<' + childTag + '>';
    if (invalidParent) {
      let info = '';
      if (ancestorTag === 'table' && childTag === 'tr') {
        info +=
          ' Add a <tbody>, <thead> or <tfoot> to your code to match the DOM tree generated by ' +
          'the browser.';
      }
      console.error(
        'In HTML, %s cannot be a child of <%s>.%s\n' +
          'This will cause a hydration error.%s',
        tagDisplayName,
        ancestorTag,
        info,
        ancestorDescription,
      );
    } else {
      console.error(
        'In HTML, %s cannot be a descendant of <%s>.\n' +
          'This will cause a hydration error.%s',
        tagDisplayName,
        ancestorTag,
        ancestorDescription,
      );
    }
    if (child) {
      // For debugging purposes find the nearest ancestor that caused the issue.
      // The stack trace of this ancestor can be useful to find the cause.
      // If the parent is a direct parent in the same owner, we don't bother.
      const parent = child.return;
      if (
        ancestor !== null &&
        parent !== null &&
        (ancestor !== parent || parent._debugOwner !== child._debugOwner)
      ) {
        runWithFiberInDEV(ancestor, () => {
          console.error(
            // We repeat some context because this log might be taken out of context
            // such as in React DevTools or grouped server logs.

Domain

Subdomains

Frequently Asked Questions

What does validateDOMNesting() do?
validateDOMNesting() is a function in the react codebase, defined in packages/react-dom-bindings/src/client/validateDOMNesting.js.
Where is validateDOMNesting() defined?
validateDOMNesting() is defined in packages/react-dom-bindings/src/client/validateDOMNesting.js at line 549.
What does validateDOMNesting() call?
validateDOMNesting() calls 4 function(s): describeAncestors, findAncestor, findInvalidAncestorForTag, isTagValidWithParent.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free