[英]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.