jsx-no-forbidden-nesting
Keep intrinsic JSX elements out of parents whose HTML content model forbids them.
Why
The HTML parser repairs some nestings. A <div> closes an open <p>, a nested <a> or <button> closes the outer one, the parser drops a nested <form>, and a <tr> directly in <table> gains a <tbody>. The browser then builds a DOM from server HTML that differs from the React tree, and React reports a hydration error. Other nestings parse unchanged but stay invalid HTML. Interactive content inside <a> or <button>, <label> inside <label>, and <li> outside a list give inconsistent click, focus and screen-reader behavior.
Fix
Make the two elements siblings, or change the outer element to one that accepts the inner one: <div> instead of <p> around blocks, one link per card with the other actions beside it, separate forms or the form attribute instead of nested forms, and <tbody> around table rows. Give a stray <li> a <ul>, <ol> or <menu> parent.
Examples
export const Note = () => ( <p> Saved<div>Undo</div> </p>);export const Note = () => ( <div> <p>Saved</p> <div>Undo</div> </div>);export const Card = () => ( <a href="/report"> Report <button type="button">Share</button> </a>);export const Card = () => ( <article> <a href="/report">Report</a> <button type="button">Share</button> </article>);export const Rows = ({ ids }: { ids: string[] }) => ( <table> {ids.map((id) => ( <tr key={id}> <td>{id}</td> </tr> ))} </table>);export const Rows = ({ ids }: { ids: string[] }) => ( <table> <tbody> {ids.map((id) => ( <tr key={id}> <td>{id}</td> </tr> ))} </tbody> </table>);References
- https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inbody
- https://html.spec.whatwg.org/multipage/dom.html#interactive-content
- https://github.com/facebook/react/blob/main/packages/react-dom-bindings/src/client/validateDOMNesting.js
- https://bun.com/docs/lint/rules/bun/jsx-no-forbidden-nesting