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

* 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>
This commit is contained in:
JJ Kasper 2022-03-22 13:58:55 -05:00 committed by GitHub
parent 6da769129e
commit 72478c5c80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 62 additions and 0 deletions

View file

@ -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>
)
}
```

View file

@ -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"

View file

@ -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'

View file

@ -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>
)
}

View file

@ -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()