简体   繁体   English

Next.js:错误:React.Children.only expected to receive a single React element child

[英]Next.js: Error: React.Children.only expected to receive a single React element child

I'm having a component called Nav inside components directory and it's code is some thing like below:我在components目录中有一个名为Nav的组件,它的代码如下所示:

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:但是如果我删除<Link>组件中的<a>标签,我可以查看页面,但随后在控制台中我收到警告:

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?那我在这里做错了什么?

This issue is due to the space between <Link> tag and <a> tag.此问题是由于<Link>标签和<a>标签之间的空格造成的。

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. -> <Link>只能有一个子节点,此处link和标签之间a空间被视为子节点。

-> So there are two child nodes (One is space and another is <a> tag) which is invalid and hence such error occurs. -> 所以有两个子节点(一个是空格,另一个是<a>标签)是无效的,因此会发生这种错误。

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." <Link> 和 <a> 之间的空格使您出错“未处理的运行时错误错误:多个子代被传递给带有/href ,但只支持一个子代https://nextjs.org/docs/messages/link-multiple-children打开浏览器的控制台以查看组件堆栈跟踪。”

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

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:解决方法是将其包装在a标签中:

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

If the child is <a> tag then add legacyBehavior .如果子项是<a>标记,则添加legacyBehavior it will work它会起作用

import Link from 'next/link'

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

export default Legacy

I was having the same issue there were no problems with spaces rather I was passing an integer inside Link我遇到了同样的问题,空格没有问题,而是我在链接中传递了 integer

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

I resolved this error by parsing the integer using toString() method我通过使用toString()方法解析 integer 解决了这个错误

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

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.由于经验和互联网帮助,仅损失了 30 分钟。

Im following the NextJs tutorial and space is not only the culprit.我正在关注 NextJs 教程,而空间不仅是罪魁祸首。 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
function FirstPost() {
  return (
    <>
      <h1> First Post </h1>
      <h2>
        <Link href="/">
          <a> Back to Home </a>
        </Link>
      </h2>
    </>
  );
}

This error also gets fired when there's no content in between <Link> opening and closing tags.<Link>开始和结束标记之间没有内容时,也会触发此错误。

For example:例如:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取此 React.Children.only 预计会收到单个 React 元素子错误以供使用<link>在 Next.js - Getting this React.Children.only expected to receive a single React element child error for using <Link> in Next.js 我收到“错误:React.Children.only expected to receive a single React element child.” 与 TouchableWithoutFeedback - I receive "Error: React.Children.only expected to receive a single React element child." with TouchableWithoutFeedback ReactQuill(富文本) - 错误:React.Children.only 期望接收单个 React 元素子元素 - React - ReactQuill (rich text) - Error: React.Children.only expected to receive a single React element child - React React bootstrap Tooltip抛出错误&#39;React.Children.only预计会收到一个React元素子元素。 - React bootstrap Tooltip throws error 'React.Children.only expected to receive a single React element child.' 错误:React.Children.only 期望接收单个 React 元素子元素。 福米克 - Error: React.Children.only expected to receive a single React element child. Formik 错误:React.Children.only 期望接收单个 React 元素子元素 - Error: React.Children.only expected to receive a single React element child 未捕获的错误:React.Children.only 期望接收单个 React 元素子项 - Uncaught Error: React.Children.only expected to receive a single React element child React.children.only期望收到一个react元素子Navigator - React.children.only expected to receive a single react element child Navigator WordPress的GutenBurg块-React.Children。仅预期接收一个React子元素 - Wordpress GutenBurg Block - React.Children.only expected to receive a single React element child Nextjs - Reactjs - 不变违规:React.Children.only 预期接收单个 React 元素子元素 - Nextjs - Reactjs - Invariant Violation: React.Children.only expected to receive a single React element child
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM