React warns that an <a> is nested inside another <a>
Warning: 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.
Fix it fast
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 402Match what you see, then jump to its fix
- 1
The 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
The 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
- 1
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)" src2Fix
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 // 0Confirmed by mui.com
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').length2Fix
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').lengthConfirmed by developer.mozilla.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
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 fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fix