Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update next/link error when no children are provided #35453

Merged
merged 5 commits into from Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions errors/link-no-children.md
@@ -0,0 +1,35 @@
# No children were passed to <Link>

#### Why This Error Occurred

In your application code `next/link` was used without passing a child:

For example:

```js
import Link from 'next/link'

export default function Home() {
return (
<Link href="/about"></Link>
// or
<Link href='/about' />
)
}
```

#### Possible Ways to Fix It

Make sure one child is used when using `<Link>`:

```js
import Link from 'next/link'

export default function Home() {
return (
<Link href="/about">
<a>To About</a>
</Link>
)
}
```
4 changes: 4 additions & 0 deletions errors/manifest.json
Expand Up @@ -514,6 +514,10 @@
"title": "future-webpack5-moved-to-webpack5",
"path": "/errors/future-webpack5-moved-to-webpack5.md"
},
{
"title": "link-no-children",
"path": "/errors/link-no-children.md"
},
{
"title": "link-multiple-children",
"path": "/errors/link-multiple-children.md"
Expand Down
5 changes: 5 additions & 0 deletions packages/next/client/link.tsx
Expand Up @@ -231,6 +231,11 @@ function Link(props: React.PropsWithChildren<LinkProps>) {
try {
child = React.Children.only(children)
} catch (err) {
if (!children) {
throw new Error(
`No children were pass to <Link> with \`href\` of \`${props.href}\` but one child is required https://nextjs.org/docs/messages/link-no-children`
ijjk marked this conversation as resolved.
Show resolved Hide resolved
)
}
throw new Error(
`Multiple children were passed to <Link> with \`href\` of \`${props.href}\` but only one child is supported https://nextjs.org/docs/messages/link-multiple-children` +
(typeof window !== 'undefined'
Expand Down
10 changes: 10 additions & 0 deletions test/integration/client-navigation/pages/link-no-child.js
@@ -0,0 +1,10 @@
import React from 'react'
import Link from 'next/link'

export default function Page() {
return (
<div>
Hello World. <Link href="/about"></Link>
</div>
)
}
8 changes: 8 additions & 0 deletions test/integration/client-navigation/test/index.test.js
Expand Up @@ -60,6 +60,14 @@ describe('Client Navigation', () => {
await browser.close()
})

it('should have proper error when no children are provided', async () => {
const browser = await webdriver(context.appPort, '/link-no-child')
expect(await hasRedbox(browser, true)).toBe(true)
expect(await getRedboxHeader(browser)).toContain(
'No children were pass to <Link> with `href` of `/about` but one child is required'
ijjk marked this conversation as resolved.
Show resolved Hide resolved
)
})

it('should navigate back after reload', async () => {
const browser = await webdriver(context.appPort, '/nav')
await browser.elementByCss('#about-link').click()
Expand Down