Nested anchor tag causing validateDOMNesting warning in React code
15 replies
Last updated: Nov 12, 2021
M
I'm trying to make whole article clickable, but got the warning: Warning: validateDOMNesting(...): <a> cannot appear as a descendant of <a> AND Warning: Expected server HTML to contain a matching <figure> in <a>.
components/HeroPost/index.js
components/PostPreview/index.js:
If i put Link over <h3> i will not get the warning, but then the article is not clickable, only the img and title.
I found this:
This is the code which causing the error,
Which is converted to,
So you are getting error,
To solve this just use one of the follow,
OR,
but i'm not sure how to apply this in this code, i might need a work around or structure the code differently. Hope there is someone here who can help, thanks!
components/HeroPost/index.js
import CoverImage from '../../CoverImage'; import Link from '../../Link'; import SectionSeparator from '../../SectionSeparator'; import React from 'preact/compat'; export default function HeroPost({ coverImage, slug, displayExcerpt, displayTitle, }) { return ( <div className="front-container" data-row-id='1'> <div className="row-grid-container"> <Link href={`/posts/${slug}`}> <CoverImage slug={slug} imageObject={coverImage} title={displayTitle} url={coverImage}/> <div className="title-container"> <p className="excerpt">{displayExcerpt}</p> <h3 className="title"> <a>{displayTitle}</a> </h3> </div> </Link> </div> <SectionSeparator/> </div> ); };
components/PostPreview/index.js:
import CoverImage from '../../CoverImage'; import Link from '../../Link'; import {imageBuilder} from '../../../lib/sanity'; export default function PostPreview({ title, coverImage, slug, displayTitle, displayExcerpt, }) { return ( <article> <Link href={`/posts/${slug}`}> <div className="image-container"> <CoverImage slug={slug} title={title} imageObject={coverImage} url={imageBuilder(coverImage).url()}/> </div> <p className="excerpt">{displayExcerpt}</p> <h3 className="title"> <a className='hover:underline'>{displayTitle}</a> </h3> </Link> </article> ); }
I found this:
This is the code which causing the error,
<NavLink href="#x"><Link id="RouterNavLink" style={None} to="/contact">anywords</Link></NavLink>
<a><a></a></a>
Warning: validateDOMNesting(ā¦): <a> cannot appear as a descendant of <a>
<NavLink id="RouterNavLink" style={None} to="/contact">anywords</NavLink>
<Link id="RouterNavLink" style={None} to="/contact">anywords</Link>
Nov 12, 2021, 11:21 AM
C
This seems like more of a React issue not a sanity one, but it seems like you might have an anchor
<a>tag nested within another
<a>tag.What is the code for your
Link?
Nov 12, 2021, 11:50 AM
M
The Link:
import React from 'react' import NextLink from 'next/link' interface Props { children: Array<JSX.Element | string | null> | JSX.Element | string | null className?: string href: string target?: string rel?: string onClick?: (event: React.MouseEvent<Element, MouseEvent>) => void } const Link: React.FC<Props> = ({ children, href, className, target, rel, onClick }) => { // @ts-ignore function handleClick(e) { if (e.type === 'click' || e.key === 'Enter') { onClick?.(e) } } return ( <NextLink href={href}> <a className={className} target={target} rel={rel} {...(onClick ? { onClick, onKeyPress: handleClick, role: 'link', tabIndex: 0, } : {})} > {children} </a> </NextLink> ) } export default Link
Nov 12, 2021, 11:52 AM
C
Yeah so you've got two nested
atags here. The
atag that sits in your
Linkcomponent, and then another one that lives in the
children
<Link href={`/posts/${slug}`}> <div className="image-container"> <CoverImage slug={slug} title={title} imageObject={coverImage} url={imageBuilder(coverImage).url()}/> </div> <p className="excerpt">{displayExcerpt}</p> <h3 className="title"> <a className='hover:underline'>{displayTitle}</a> </h3> </Link>
Nov 12, 2021, 11:53 AM
C
to fix, just change to
<a className='hover:underline'>{displayTitle}</a>
<span className='hover:underline'>{displayTitle}</span>
Nov 12, 2021, 11:54 AM
C
and do the same anywhere else you've used a nested anchor tag
Nov 12, 2021, 11:54 AM
M
i'm still getting the warning, unfortunately š
and
return ( <div className="front-container" data-row-id='1'> <div className="row-grid-container"> <Link href={`/posts/${slug}`}> <CoverImage slug={slug} imageObject={coverImage} title={displayTitle} url={coverImage}/> <div className="title-container"> <p className="excerpt">{displayExcerpt}</p> <h3 className="title"> <span>{displayTitle}</span> </h3> </div> </Link> </div> <SectionSeparator/> </div> ); };
return ( <article> <Link href={`/posts/${slug}`}> <div className="image-container"> <CoverImage slug={slug} title={title} imageObject={coverImage} url={imageBuilder(coverImage).url()}/> </div> <p className="excerpt">{displayExcerpt}</p> <h3 className="title"> <span className='hover:underline'>{displayTitle}</span> </h3> </Link> </article> ); }
Nov 12, 2021, 11:57 AM
C
The
validateDOMNestingwarning?
Nov 12, 2021, 11:58 AM
C
It looks like you're passing a url to
CoverImagedoes this component contain another anchor tag?
Nov 12, 2021, 11:59 AM
M
yes, Warning: validateDOMNesting(...): <a> cannot appear as a descendant of <a>. and Warning: Expected server HTML to contain a matching <figure> in <a>.
Nov 12, 2021, 11:59 AM
C
Ah wait that's the image url
Nov 12, 2021, 11:59 AM
C
Your best bet is to check the rendered code to see if there's another <a> tag nested anywhere
Nov 12, 2021, 12:00 PM
C
You're passing a slug to
CoverImageso maybe there's an
atag in there afterall
Nov 12, 2021, 12:00 PM
M
yes, thanks. It was the CoverImage. I changed it to span too and that fixed my problem. Thank you!
Nov 12, 2021, 12:01 PM
C
No problem š in future you're probably better asking for help in a dedicated web dev server such as Reactiflux
https://www.reactiflux.com/
https://www.reactiflux.com/
Nov 12, 2021, 12:03 PM
M
Awesome, thanks!
Nov 12, 2021, 12:04 PM
Sanityā build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.