简体   繁体   English

React.Children.only 期望接收单个 React 元素孩子。?

[英]React.Children.only expected to receive a single React element child.?

I have a DupRoute component that creates an array of Routes, when I add a DupRoute component to the Switch I get an Error, React.Children.only expected to receive a single React element child.我有一个创建路由数组的 DupRoute 组件,当我将 DupRoute 组件添加到 Switch 时,我得到一个错误,React.Children.only 期望接收一个 React 元素子元素。 Here is my cat.这是我的猫。 https://codesandbox.io/s/74548ypmk6 https://codesandbox.io/s/74548ypmk6

The issue says, somewhere in your code, you are having multiple components as children when it only accepts 1.问题是,在您的代码中的某处,当它只接受 1 时,您将多个组件作为子组件。

If I am not wrong, Route component inside of duproute.js doesn't accept multiple components.如果我没记错的话,duproute.js 中的 Route 组件不接受多个组件。 You have {" "} {text}{" "} inside of Route which I think is invalid.您在 Route 中有{" "} {text}{" "}我认为这是无效的。

Try changing尝试改变

{" "}
{text}
{" "}

to

<Fragment>
     {" "}
     {text}
     {" "}
</Fragment>

Fragment can be safely used as it doesn't add any extra HTML element in our DOM. Fragment 可以安全地使用,因为它不会在我们的 DOM 中添加任何额外的 HTML 元素。

Route component's responsibility is to render UI (that we give as part of props to it) when a location matches the route's path.路由组件的职责是在位置与路由的路径匹配时呈现 UI(我们作为道具的一部分提供给它)。

As you can see route is a complaint about to have multiple children to render it, So you need to use React Fragment to combine those multiple children like正如你所看到的,路由是一个关于有多个孩子来渲染它的抱怨,所以你需要使用React Fragment来组合这些多个孩子,比如

 <Route
          key={id}
          component={component}
          render={render}
          children={children}
          path={path}
          exact={exact}
          strict={strict}
          location={location}
          sensitive={sensitive}
        >
         <>
          {" "}
          {text}{" "}
          </>
        </Route>

暂无
暂无

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

相关问题 我收到“错误: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 React-google-maps信息窗口`React.Children.only仅预期接收单个React元素的子级。 - React-google-maps infowindow `React.Children.only expected to receive a single React element child.` 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 元素子元素。 插入组件时出错 - React.Children.only expected to receive a single React element child. Error when inserting component 是什么导致“React.Children.only期望在一个apollo客户端呈现组件中接收单个React元素子。”错误 - What causes an “React.Children.only expected to receive a single React element child.” error in an apollo client rendered component Invariant Violation: Invariant Violation: React.Children.only 期望接收单个 React 元素子元素。 我不知道为什么? - Invariant Violation: Invariant Violation: React.Children.only expected to receive a single React element child. I don't know why? React.Children.only只希望收到一个React元素chil - React.Children.only expected to receive a single React element chil ReactQuill(富文本) - 错误:React.Children.only 期望接收单个 React 元素子元素 - React - ReactQuill (rich text) - Error: React.Children.only expected to receive a single React element child - React React Router v4 - 错误:React.Children.only预计会收到一个React元素子元素 - React Router v4 - Error: React.Children.only expected to receive a single React element child
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM