Skip to content

Commit

Permalink
Update next/link error when no children are provided (#35453)
Browse files Browse the repository at this point in the history
* Update next/link error when no children are provided

* update manifest

* Apply suggestions from code review

Co-authored-by: Balázs Orbán <info@balazsorban.com>
  • Loading branch information
ijjk and balazsorban44 committed Mar 22, 2022
1 parent 6da7691 commit 72478c5
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
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 passed to <Link> with \`href\` of \`${props.href}\` but one child is required https://nextjs.org/docs/messages/link-no-children`
)
}
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 passed to <Link> with `href` of `/about` but one child is required'
)
})

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

0 comments on commit 72478c5

Please sign in to comment.