diff --git a/errors/link-no-children.md b/errors/link-no-children.md new file mode 100644 index 000000000000..103018f1f80e --- /dev/null +++ b/errors/link-no-children.md @@ -0,0 +1,35 @@ +# No children were passed to + +#### 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 ( + + // or + + ) +} +``` + +#### Possible Ways to Fix It + +Make sure one child is used when using ``: + +```js +import Link from 'next/link' + +export default function Home() { + return ( + + To About + + ) +} +``` diff --git a/errors/manifest.json b/errors/manifest.json index d80ef5787008..ae234227e647 100644 --- a/errors/manifest.json +++ b/errors/manifest.json @@ -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" diff --git a/packages/next/client/link.tsx b/packages/next/client/link.tsx index 9bb634d251ad..379d21123e15 100644 --- a/packages/next/client/link.tsx +++ b/packages/next/client/link.tsx @@ -231,6 +231,11 @@ function Link(props: React.PropsWithChildren) { try { child = React.Children.only(children) } catch (err) { + if (!children) { + throw new Error( + `No children were passed to 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 with \`href\` of \`${props.href}\` but only one child is supported https://nextjs.org/docs/messages/link-multiple-children` + (typeof window !== 'undefined' diff --git a/test/integration/client-navigation/pages/link-no-child.js b/test/integration/client-navigation/pages/link-no-child.js new file mode 100644 index 000000000000..e3786f932ee7 --- /dev/null +++ b/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 ( +
+ Hello World. +
+ ) +} diff --git a/test/integration/client-navigation/test/index.test.js b/test/integration/client-navigation/test/index.test.js index 2414ac400a1a..b0fc4128bf95 100644 --- a/test/integration/client-navigation/test/index.test.js +++ b/test/integration/client-navigation/test/index.test.js @@ -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 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()