There's plenty of information about how to use Next.js' Link
module if the child itself has an <a>
tag within it (passHref
), but there seems to be no indication that this should be needed for child component that aren't creating their own <a>
tag. But I have run into a situation where a "decorator" component being a child of a Link
component causes the link to not get rendered at all.
import React from 'react'
import Link from 'next/link'
import type { NextPage } from 'next'
interface Props {
children: string
}
// A simple functional component that transforms its child to more readable form
const HexString = ({ children: hex }: Props) => {
const shortHex = hex.substring(0, 8) + '...' + hex.substring(hex.length - 6)
return (
<span title={hex} className="hex-string">
{shortHex}
</span>
)
}
// Page component wishing to use that decorator inside a Link
const MyPage: NextPage = () => {
return (
<div>
<Link href="/index">
<HexString>0x0102030405060708090a0b0c0d0e0f</HexString>
</Link>
</div>
)
}
export default MyPage
What gets rendered to the page is only the hex-string
span and its contents. The <a>
tag the Link
is supposed to add is not in the DOM.
How can I get the Link
component to properly render an anchor tag around this styled span tag from another component?