2023-07-05 15:11:16 +02:00
|
|
|
---
|
|
|
|
title: 'Link `passHref`'
|
|
|
|
---
|
2022-06-14 04:17:42 +02:00
|
|
|
|
|
|
|
> Ensure `passHref` is used with custom `Link` components.
|
2021-05-10 20:35:11 +02:00
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
## Why This Error Occurred
|
2021-05-10 20:35:11 +02:00
|
|
|
|
|
|
|
`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.
|
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
## Possible Ways to Fix It
|
2021-05-10 20:35:11 +02:00
|
|
|
|
|
|
|
If you're using a custom component that wraps an `<a>` tag, make sure to add `passHref`:
|
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
```jsx filename="nav-link.js"
|
2021-05-10 20:35:11 +02:00
|
|
|
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
|
|
|
|
```
|
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
## Useful Links
|
2021-05-10 20:35:11 +02:00
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
- [next/link - Custom Component](/docs/pages/api-reference/components/link#if-the-child-is-a-custom-component-that-wraps-an-a-tag)
|