日本語

Fixing cannot be a descendant of in React

​​React​HTML,Troubleshooting,TypeScript,Vite

The message In HTML, <div> cannot be a descendant of <p>. in the React console appears when a <div> is rendered inside a <p>. HTML only allows phrasing content (such as <span> or <strong>) inside a <p>. Either change the outer <p> to a <div>, or change the inner <div> to a <span>, and the error goes away.

This article covers the cause and the fix for a case where a small component, such as a notice or a badge, is placed inside a paragraph of description text and the browser's developer tools console shows this error. I reproduced the error in a test environment and included the message exactly as it appeared. I also confirmed that the message disappears after the fix.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, React 19.3.1, Vite 8.3.8, and TypeScript 7.0.2. I created the project with the react-ts template from npm create vite@latest and ran it with the development server from npm run dev (which runs vite). The browser was Chromium from Playwright 1.63.0 (headless), and I extracted the console text with a script. I also reproduced the message and both fixes with the same code on React 18.3.1. I did not test Next.js or other frameworks, browsers other than Chromium, or Windows and macOS.

The code that caused the error

In src/App.tsx, I placed a notice component, Notice, inside the <p> of App. Notice returns a <div>. In the JSX, no <div> appears inside the <p> at first glance, which makes this easy to miss.

tsx
function Notice() {
  return <div className="notice">在庫が少なくなっています</div>
}

function App() {
  return (
    <main>
      <h1>商品の説明</h1>
      <p>
        この商品は人気です。
        <Notice />
      </p>
    </main>
  )
}

export default App

src/main.tsx is unchanged from the template. It uses createRoot to render <App /> inside <StrictMode>.

The message in the console

When I opened the page, the text displayed normally. The console then showed the following two errors (console.error).

text
In HTML, <div> cannot be a descendant of <p>.
This will cause a hydration error.

  <App>
    <main>
      <h1>
>     <p>
        <Notice>
>         <div className="notice">
text
<p> cannot contain a nested <div>.
See this log for the ancestor stack trace.

The second half of the first message is the stack from <App> down to the element where the error occurred. The two lines that start with > are the problematic combination. You can see that the outer <p> belongs to App, and the inner <div> is inside Notice. The second message describes the same problem from the side of the outer <p>.

When I wrote the <div> directly inside the <p> without going through Notice, I got only one error, with the same opening text as the first message. The stack in that case had two lines: a > <p> line, and a > <div> line indented below it. In the test environment with Vite 8.3.8, the same text also appeared in the terminal running the development server, after [vite] (client) [console.error].

Elements that can and cannot go inside a p

The same error occurs with elements other than <div>. I rendered each element inside a <p> and counted the console errors. The results are in the following table.

Element inside <p> Errors First message
<div> 1 In HTML, <div> cannot be a descendant of <p>.
<section> 1 In HTML, <section> cannot be a descendant of <p>.
<ul> 1 In HTML, <ul> cannot be a descendant of <p>.
<h2> 1 In HTML, <h2> cannot be a descendant of <p>.
<form> 1 In HTML, <form> cannot be a descendant of <p>.
<p> 1 In HTML, <p> cannot be a descendant of <p>.
<span>, <strong>, <a>, <button>, <img>, <input>, <label> 0 None

The elements that caused no error are "phrasing content" in MDN's terms, meaning elements designed to sit within text. MDN's <p> page states that the permitted content of a <p> is phrasing content only. The MDN content categories page lists which elements count as phrasing content.

Why the message mentions a hydration error

The second line of the message says This will cause a hydration error. When I used a setup where the server generates the HTML and the browser then attaches React to it (hydration), I did get a hydration error.

When React renders in the browser, it builds the DOM exactly as the JSX describes. In my test, the DOM after rendering did have a <div> inside a <p>. However, when the same content is loaded as HTML, the browser builds a different DOM. As MDN's <p> page explains, a <p> closes automatically when a block-level element such as a <div> appears before its closing tag.

When I converted App to HTML with renderToString, I got the following string.

html
<main><h1>商品の説明</h1><p>この商品は人気です。<div class="notice">在庫が少なくなっています</div></p></main>

I loaded this into a page and opened it with JavaScript disabled. The DOM the browser built was as follows. The <p> closed just before the <div>, so the <div> ended up outside the <p>. The final </p> produced an empty <p></p>.

html
<main><h1>商品の説明</h1><p>この商品は人気です。</p><div class="notice">在庫が少なくなっています</div><p></p></main>
Two DOM trees. The top one is the DOM React rendered: inside a p under main, there is text followed by a div. The bottom one is the DOM the browser built from the HTML: under main there are a p, a div, and an empty p in sequence
Difference between the DOM React rendered in the browser and the DOM the browser built from the HTML generated on the server (h1 omitted)

When I attached App to this HTML with hydrateRoot, the console showed the two errors above. It also showed the following message as an uncaught error. I have omitted the bulleted list of possible causes at the end.

text
Hydration failed because the server rendered HTML didn't match the client. As a result this tree will be regenerated on the client. This can happen if a SSR-ed Client Component used:

The list I omitted also included Invalid HTML tag nesting.

The reason is that the DOM built from the server HTML did not match the structure React tried to render. After this, React re-rendered on the client, and the final DOM had a <div> inside a <p>. React's hydrateRoot reference says that although React can sometimes recover from hydration errors, you should fix them like any other bug.

Two fixes: make the outer element a div, or the inner element a span

With either fix, the error disappeared when I saved the file while the development server was running. After the fix, the console showed zero errors, even after reloading the page.

The first fix is to change the outer <p> to a <div>. Use this when the component contains content that cannot go inside a paragraph, such as a list or a heading.

tsx
function App() {
  return (
    <main>
      <h1>商品の説明</h1>
      <div>
        この商品は人気です。
        <Notice />
      </div>
    </main>
  )
}

The DOM after rendering is as follows.

html
<main><h1>商品の説明</h1><div>この商品は人気です。<div class="notice">在庫が少なくなっています</div></div></main>

The second fix is to change the <div> returned by the inner component to a <span>. If the component only outputs a short phrase, you can keep the outer <p>.

tsx
function Notice() {
  return <span className="notice">在庫が少なくなっています</span>
}

The DOM after rendering is as follows.

html
<main><h1>商品の説明</h1><p>この商品は人気です。<span class="notice">在庫が少なくなっています</span></p></main>
Point

When you place a component inside a <p>, check which element the component ultimately returns. If it returns a <div>, you get the same error even though the JSX looks fine.

Type checking and production builds do not catch it

I ran npx tsc -b on App.tsx while the error was present, and type checking reported no errors. TypeScript does not treat this combination as a type error.

I built with npx vite build and opened the page with npx vite preview. The console showed no errors. The DOM still had a <div> inside a <p>, the same as in development. When I inspected the installed react-dom, the message text appeared only in the development file (react-dom-client.development.js) and not in the production file (react-dom-client.production.js). If you only check production builds, you will not notice this problem.

The message in React 18

When I rendered the same App.tsx with React 18.3.1, the console showed one message. If you search for this error in a React 18 project, search for this text.

text
Warning: validateDOMNesting(...): <div> cannot appear as a descendant of <p>.
    at div
    at Notice
    at p
    at main
    at App

In this React 18.3.1 project, both fixes (changing the outer <p> to a <div>, and changing the inner <div> to a <span>) also brought the error count to zero. It stayed at zero after reloading the page.

​Posted by 86