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

Add helpful error for link with multiple children #25657

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions errors/link-multiple-children.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Multiple children were passed to <Link>

#### Why This Error Occurred

In your application code multiple children were passed to `next/link` but only one child is supported:

For example:

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

export default function Home() {
return (
<Link href="/about">
<a>To About</a>
<a>Second To About</a>
</Link>
)
}
```

#### Possible Ways to Fix It

Make sure only 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>
)
}
```
16 changes: 15 additions & 1 deletion packages/next/client/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,21 @@ function Link(props: React.PropsWithChildren<LinkProps>) {
}

// This will return the first child, if multiple are provided it will throw an error
const child: any = Children.only(children)
let child: any
if (process.env.NODE_ENV === 'development') {
try {
child = Children.only(children)
} catch (err) {
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'
? "\nOpen your browser's console to view the Component stack trace."
: '')
)
}
} else {
child = Children.only(children)
}
const childRef: any = child && typeof child === 'object' && child.ref

const [setIntersectionRef, isVisible] = useIntersection({
Expand Down
28 changes: 28 additions & 0 deletions test/acceptance/ReactRefreshLogBox.dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,34 @@ test('logbox: anchors links in error messages', async () => {
await cleanup()
})

test('<Link> with multiple children', async () => {
const [session, cleanup] = await sandbox()

console.log({ SANDBOX: session.sandboxDirectory })
await session.patch(
'index.js',
`
import Link from 'next/link'

export default function Index() {
return (
<Link href="/">
<p>One</p>
<p>Two</p>
</Link>
)
}
`
)

expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(
`"Error: Multiple children were passed to <Link> with \`href\` of \`/\` but only one child is supported https://nextjs.org/docs/messages/link-multiple-children"`
)

await cleanup()
})

test('<Link> component props errors', async () => {
const [session, cleanup] = await sandbox()

Expand Down