Skip to content
Sarj Code Standards

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

Before — flagged Report a block element inside a paragraph
src/note.tsx
export const Note = () => (
<p>
Saved<div>Undo</div>
</p>
);
After — preferred Use a block container around block content
src/note.tsx
export const Note = () => (
<div>
<p>Saved</p>
<div>Undo</div>
</div>
);
Before — flagged Report interactive content inside a link
src/card.tsx
export const Card = () => (
<a href="/report">
Report
<button type="button">Share</button>
</a>
);
After — preferred Keep card actions beside the card link
src/card.tsx
export const Card = () => (
<article>
<a href="/report">Report</a>
<button type="button">Share</button>
</article>
);
Before — flagged Report mapped rows directly inside a table
src/table.tsx
export const Rows = ({ ids }: { ids: string[] }) => (
<table>
{ids.map((id) => (
<tr key={id}>
<td>{id}</td>
</tr>
))}
</table>
);
After — preferred Wrap mapped rows in a table body
src/table.tsx
export const Rows = ({ ids }: { ids: string[] }) => (
<table>
<tbody>
{ids.map((id) => (
<tr key={id}>
<td>{id}</td>
</tr>
))}
</tbody>
</table>
);

References