43

I'm having a component called Nav inside components directory and it's code is some thing like below:

import Link from 'next/link';

const Nav = () => {
    return(
        <div>
            <Link href="/">  <a> Home </a> </Link>
            <Link href="/about"> <a> About </a>  </Link>
        </div>
    )
}

export default Nav;

This gives me the error:

Error: React.Children.only expected to receive a single React element child.

But if I remove the <a> tags within <Link> components, I can view the pages, but then in the console I'm getting a warning of:

Warning: You're using a string directly inside <Link>. This usage has been deprecated. Please add an <a> tag as child of <Link>

So what am I doing wrong here?

7
  • Looks good. Are you sure you are receiveng the children error here and not somewhere else.. Commented May 7, 2020 at 6:49
  • 14
    Remove the space between Link tag and a tag.. And give it like, <Link href="/"><a> Home </a></Link> <Link href="/about"><a> About </a></Link> Commented May 7, 2020 at 6:51
  • 3
    wow thanks @ManirajMurugan, It solved the issue. But that's strange having a space causing that issue. Now I Even tried having <a>s in seperate lines too, that also didn't cause any issue. Any way thanks :) Commented May 7, 2020 at 6:56
  • 2
    @AshanPriyadarshana, The next line is not considered as a space but if there is a space between both the tags then that is considered as child node(s) .. Posted answer with explanation with reason of removing white space between.. Hope it helps.. Commented May 7, 2020 at 7:04
  • 1
    I had a comment inside the Link tag, and then that was being considered as a Child Node, breaking my whole app. Weird Stuff... Commented Mar 9, 2021 at 13:50

9 Answers 9

84

This issue is due to the space between <Link> tag and <a> tag.

So change your code like,

        <div>
            <Link href="/"><a> Home </a></Link>
            <Link href="/about"><a> About </a></Link>
        </div>

Reason for the change:

-> The <Link> can have only one child node and here the space between the link and a tag are considered as a child nodes.

-> So there are two child nodes (One is space and another is <a> tag) which is invalid and hence such error occurs.

Sign up to request clarification or add additional context in comments.

2 Comments

In my case I think the culprit was a space after a tag, followed by a note: (</div> {/* So what this does is... */}) THANK YOU. Those <React.Fragment>/<></> and <TransitionGroup> tags were not doing the trick
I was able to solve it with this solution, but I still need more clarification as to why spaces are considered as child nodes.
7

Space between < Link > and < a > makes you error "Unhandled Runtime Error Error: Multiple children were passed to with href of / but only one child is supported https://nextjs.org/docs/messages/link-multiple-children Open your browser's console to view the Component stack trace."

Remove the space and it work fine.

import Link from "next/link";

const NavBar = () => {
    return (
        <nav>
            
           <Link href="/"><a>Home </a></Link>
           <Link href="/About"><a>About </a></Link>
            <Link href="/Contact"><a>Contact </a></Link>
        </nav>
    )
}

export default NavBar

Comments

5

I had the same issue as I was doing:

<Link href={`/tutorial/${tutorial.slug}`}>{tutorial.title}</Link>

The fix was to wrap it in an a tag:

<Link href={`/tutorial/${tutorial.slug}`}><a>{tutorial.title}</a></Link>

Comments

2

If the child is <a> tag then add legacyBehavior. it will work

import Link from 'next/link'

function Legacy() {
  return (
    <Link href="/about" legacyBehavior>
      <a>About Us</a>
    </Link>
  )
}

export default Legacy

Comments

1

I had a space that was considered as an undefined component

<Link href={to}><a className={'abc'}>{children}</a> </Link>

Removing the space, it was ok. 30 minutes lost only, thanks to experience and internet help.

Comments

1

I was having the same issue there were no problems with spaces rather I was passing an integer inside Link

    <Link href={{
           pathname: `/dashboard/people`,
          query: { idPerson: 123 }}}>
       {123}
   </Link>

I resolved this error by parsing the integer using toString() method

    <Link href={{
         pathname: `/dashboard/people`,
         query: { idPerson: 123 }}}>
      {(123).toString()}
   </Link>

Comments

1

Check if inner content is empty

In addition to the scenarios covered by other answers, this error also gets fired when there's no content in between <Link> opening and closing tags.

For example:

<Link href='/'></Link>  // i am an error

Comments

1

I landed here because I was having a similar issue in my Next.js app, but I would get a 500 error in browser console and the React.Children.only error in my terminal whenever I refreshed manually refreshed my app on any page.

The terminal output would provide a huge stack trace that pointed to next-server in my node_modules (node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js).

As it turns out, I was using styled-components with a boilerplate Next.js app setup by NX, and in my global app layout (src/app/layout.tsx), I had the StyledComponentsRegistry component as a parent of my main application, like so:

const RootLayout = ({ children }) => (
  <html lang="en">
    <body>
      <StyledComponentsRegistry>
        <>
          <div>header</div>
          <div>main content</div>
          <div>footer</div>
        </>
      </StyledComponentsRegistry>
    </body>
  </html>
);

It turns out that React.Children.only, and by extension StyledComponentsRegistry, did not consider the Fragment (<>) the child component since it's not rendered in the DOM when the page is built.

So instead, I replaced the wrapping Fragment (<>) with a div (<div>) and the error went away!

In other words, it looked like:

const RootLayout = ({ children }) => (
  <html lang="en">
    <body>
      <StyledComponentsRegistry>
        <div> {/* changed this <> to a <div> */}
          <div>header</div>
          <div>main content</div>
          <div>footer</div>
        </div> {/* changed this </> to a </div> */}
      </StyledComponentsRegistry>
    </body>
  </html>
);

(Hope this helps someone, and sorry to piggyback off an only somewhat related issue! There were no other questions addressing mine out there.)

Comments

0

Im following the NextJs tutorial and space is not only the culprit. Semi colon also.

// without space, this will also not work because of the semi-colon
    function FirstPost() {
      return (
        <>
          <h1> First Post </h1>
          <h2>
            <Link href="/">
              <a>Back to Home</a>;
            </Link>
          </h2>
        </>
      );

// with space but without semi-colon will not also work
function FirstPost() {
  return (
    <>
      <h1> First Post </h1>
      <h2>
        <Link href="/">
          <a> Back to Home </a>
        </Link>
      </h2>
    </>
  );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.