rsnext/errors/link-passhref.mdx
Delba de Oliveira 44d1a1cb15
docs: Migrate error messages to MDX and App Router. (#52038)
This PR is part of a larger effort to migrate error messages to MDX and
use App Router: https://github.com/vercel/front/pull/23459
2023-07-05 06:11:16 -07:00

36 lines
842 B
Text

---
title: 'Link `passHref`'
---
> Ensure `passHref` is used with custom `Link` components.
## Why This Error Occurred
`passHref` was not used for a `Link` component that wraps a custom component. This is needed in order to pass the `href` to the child `<a>` tag.
## Possible Ways to Fix It
If you're using a custom component that wraps an `<a>` tag, make sure to add `passHref`:
```jsx filename="nav-link.js"
import Link from 'next/link'
import styled from 'styled-components'
const StyledLink = styled.a`
color: red;
`
function NavLink({ href, name }) {
return (
<Link href={href} passHref>
<StyledLink>{name}</StyledLink>
</Link>
)
}
export default NavLink
```
## Useful Links
- [next/link - Custom Component](/docs/pages/api-reference/components/link#if-the-child-is-a-custom-component-that-wraps-an-a-tag)