Skip to content

Commit 1630633

Browse files
committed
Improve Link documentation
1 parent 999b8ce commit 1630633

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

docs/api-reference/next/link.md

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,36 @@ const pids = ['id1', 'id2', 'id3']
7474
}
7575
```
7676

77-
## Example with `React.forwardRef`
77+
## If the child is a custom component that wraps an `<a>` tag
7878

79-
If the child component in `Link` is a function component, you'll need to wrap it in [`React.forwardRef`](https://reactjs.org/docs/react-api.html#reactforwardref) like in the following example:
79+
If the child of `Link` is a custom component that wraps an `<a>` tag, you must add `passHref` to `Link`. This is necessary if you’re using libraries like [styled-components](https://styled-components.com/). Without this, the `<a>` tag will not have the `href` attribute, which might hurt your site’s SEO.
80+
81+
```jsx
82+
import Link from 'next/link'
83+
import styled from 'styled-components'
84+
85+
const RedLink = styled.a`
86+
color: red;
87+
`
88+
89+
function NavLink({ href, name }) {
90+
return (
91+
<Link href={href} passHref>
92+
<RedLink>{name}</RedLink>
93+
</Link>
94+
)
95+
}
96+
97+
export default NavLink
98+
```
99+
100+
> **Note:** If you’re using [emotion](https://emotion.sh/)’s JSX pragma feature (`@jsx jsx`), you must use `passHref` even if you use an `<a>` tag directly.
101+
102+
## If the child is a function component
103+
104+
If the child of `Link` is a function component, in addition to using `passHref`, you must wrap the component in [`React.forwardRef`](https://reactjs.org/docs/react-api.html#reactforwardref):
80105

81106
```jsx
82-
import React from 'react'
83107
import Link from 'next/link'
84108

85109
// `onClick`, `href`, and `ref` need to be passed to the DOM element
@@ -94,7 +118,7 @@ const MyButton = React.forwardRef(({ onClick, href }, ref) => {
94118

95119
function Home() {
96120
return (
97-
<Link href="/about">
121+
<Link href="/about" passHref>
98122
<MyButton />
99123
</Link>
100124
)
@@ -147,29 +171,6 @@ The default behavior of the `Link` component is to `push` a new URL into the `hi
147171

148172
The child of `Link` is `<img>` instead of `<a>`. `Link` will send the `onClick` property to `<img>` but won't pass the `href` property.
149173

150-
## Forcing `Link` to expose `href` to its child
151-
152-
If the child is an `<a>` tag and doesn't have a `href` attribute we specify it so that the repetition is not needed by the user. However, sometimes, you’ll want to pass an `<a>` tag inside of a wrapper and `Link` won’t recognize it as a _hyperlink_, and, consequently, won’t transfer its `href` to the child.
153-
154-
In cases like that, you can add the `passHref` property to `Link`, forcing it to expose its `href` property to the child. Take a look at the following example:
155-
156-
```jsx
157-
import Link from 'next/link'
158-
import Unexpected_A from 'third-library'
159-
160-
function NavLink({ href, name }) {
161-
return (
162-
<Link href={href} passHref>
163-
<Unexpected_A>{name}</Unexpected_A>
164-
</Link>
165-
)
166-
}
167-
168-
export default NavLink
169-
```
170-
171-
> **Please note**: using a tag other than `<a>` and failing to pass `passHref` may result in links that appear to navigate correctly, but, when being crawled by search engines, will not be recognized as links (owing to the lack of `href` attribute). This may result in negative effects on your sites SEO.
172-
173174
## Disable scrolling to the top of the page
174175

175176
The default behavior of `Link` is to scroll to the top of the page. When there is a hash defined it will scroll to the specific id, just like a normal `<a>` tag. To prevent scrolling to the top / hash `scroll={false}` can be added to `Link`:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy