Unverified Commit 72478c5c authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

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: 's avatarBalázs Orbán <info@balazsorban.com>
parent 6da76912
# 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>
)
}
```
...@@ -514,6 +514,10 @@ ...@@ -514,6 +514,10 @@
"title": "future-webpack5-moved-to-webpack5", "title": "future-webpack5-moved-to-webpack5",
"path": "/errors/future-webpack5-moved-to-webpack5.md" "path": "/errors/future-webpack5-moved-to-webpack5.md"
}, },
{
"title": "link-no-children",
"path": "/errors/link-no-children.md"
},
{ {
"title": "link-multiple-children", "title": "link-multiple-children",
"path": "/errors/link-multiple-children.md" "path": "/errors/link-multiple-children.md"
......
...@@ -231,6 +231,11 @@ function Link(props: React.PropsWithChildren<LinkProps>) { ...@@ -231,6 +231,11 @@ function Link(props: React.PropsWithChildren<LinkProps>) {
try { try {
child = React.Children.only(children) child = React.Children.only(children)
} catch (err) { } 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( 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` + `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' (typeof window !== 'undefined'
......
import React from 'react'
import Link from 'next/link'
export default function Page() {
return (
<div>
Hello World. <Link href="/about"></Link>
</div>
)
}
...@@ -60,6 +60,14 @@ describe('Client Navigation', () => { ...@@ -60,6 +60,14 @@ describe('Client Navigation', () => {
await browser.close() 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 () => { it('should navigate back after reload', async () => {
const browser = await webdriver(context.appPort, '/nav') const browser = await webdriver(context.appPort, '/nav')
await browser.elementByCss('#about-link').click() await browser.elementByCss('#about-link').click()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment