Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

React warns that an <a> is nested inside another <a>

ErrorWarning: validateDOMNesting(...): <a> cannot appear as a descendant of <a>.

Short answer

HTML does not allow a link inside a link. React Router's Link renders a real <a>, and so do MUI's Button with href and Link components. Wrapping one in the other creates two nested anchors. Render one element that is the link, by passing the router Link as the component prop, or remove the inner link.

  • Applies to React 19, React Router 7, MUI 7
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    The warning prints a component stack under it. Find the first anchor-rendering component in that stack, then inspect the element in the browser's Elements panel and look for an <a> inside an <a>.

    grep -rnE "<(Link|NavLink|a)[ >]" src | head -n 40
  2. 2Match what you see, then jump to its fix

    1. 1The stack shows Link from react-router around a Button, IconButton or ListItemButton that has an hrefA router Link wraps an MUI Button (or another link-like component)Fix
    2. 2The outer element is a Link around a whole card, and the card has a 'Read more' link or a title link insideA clickable card or row contains its own linkFix

What does the error look like?

  • Warning: validateDOMNesting(...): <a> cannot appear as a descendant of <a>.

Cause 1 of 2

A router Link wraps an MUI Button (or another link-like component)

1Check

The MDN page for the anchor element says an <a> may not have another <a> or any interactive content as a descendant. A Link plus a button that renders an anchor breaks that rule.

grep -rnE "<Link[^>]*>\s*<(Button|IconButton|ListItemButton)" src

2Fix

Use a single element. MUI's routing guide shows passing React Router's Link to the component prop, which makes the MUI component render the router link itself.

import { Link as RouterLink } from 'react-router';
import Button from '@mui/material/Button';

// before: <Link to="/"><Button href="/">Home</Button></Link>
<Button component={RouterLink} to="/">
  Home
</Button>

3Verify

Reload and open the page. The warning is gone and the Elements panel shows one <a> for the button.

document.querySelectorAll('a a').length // 0

Confirmed by mui.com

Did this fix it?

Cause 2 of 2

A clickable card or row contains its own link

1Check

The same MDN rule applies: an anchor's descendants cannot be interactive content, which includes another link. A card-sized Link with an inner link or button is invalid.

document.querySelectorAll('a a, a button').length

2Fix

Keep one link per card. Make the card a plain container, put the main Link on the title, and keep any other link as a sibling of it, never inside it.

<article className="card">
  <h3><Link to={`/posts/${post.id}`}>{post.title}</Link></h3>
  <p>{post.summary}</p>
  <a href={post.source}>Source</a>
</article>

3Verify

Run the query again in the console: it returns 0.

document.querySelectorAll('a a, a button').length

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Use the component prop with RouterLink for MUI components, not a router Link wrapped around them.
  • Never put a link or a button inside a Link.
  • Add the check document.querySelectorAll('a a, a button') to your UI test so a nested anchor fails the build.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

React warns that a div is nested inside a p tag

Text content does not match server-rendered HTML

A p element may contain only phrasing content, and the HTML parser closes a paragraph as soon as a div, list or other block element starts.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.