[英]Error React.Children.only expected to receive a single React element child
Not entirely sure what went wrong in my app here.不完全确定我的应用程序出了什么问题。 I'm using create-react-app, and I'm trying to render all of my components into the respective root div.
我正在使用 create-react-app,并且我正在尝试将我的所有组件渲染到相应的根 div 中。 Problem is, I'm able to render all of the components onto the page except the very last one, the Score component.
问题是,我能够将所有组件渲染到页面上,除了最后一个 Score 组件。 I've even tried throwing that component into a div and I'm still getting the following issue:
我什至尝试将该组件放入 div 中,但仍然遇到以下问题:
'React.Children.only expected to receive a single React element child'. 'React.Children.only 期望接收单个 React 元素孩子'。
What exactly does this mean?这到底是什么意思?
Here's my App.js structure.这是我的 App.js 结构。
render() {
return (
<div className="App">
<div className="App-header">
<h2>BAO BAO BAO</h2>
</div>
{this.state.result ? this.renderResult() : this.renderTest()}
<Baoquestion />
<AnswerChoices />
<BaoScore />
<Score />
</div>
);
}
export default App;
Content of Score.js Score.js 的内容
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import CSSTransitionGroup from 'react-transition-group/CSSTransitionGroup';
function Score(props) {
return (
<div>
<CSSTransitionGroup
className="container result"
transitionName="test"
transitionEnterTimeout={500}
transitionLeaveTimeout={300}>
>
<div>
You prefer <strong>{props.testScore}</strong>!
</div>
</CSSTransitionGroup>
</div>
);
}
Score.propTypes = {
quizResult: PropTypes.string,
};
export default Score;
From react-transition-group documentation :来自react-transition-group 文档:
You must provide the key attribute for all children of CSSTransitionGroup, even when only rendering a single item.
您必须为 CSSTransitionGroup 的所有子项提供 key 属性,即使仅呈现单个项目也是如此。 This is how React will determine which children have entered, left, or stayed.
这就是 React 将如何确定哪些孩子已经进入、离开或留下。
Please then add a key, even a static one, for the component you render inside the transition group:然后请为您在过渡组中渲染的组件添加一个键,甚至是静态键:
<CSSTransitionGroup
className="container result"
transitionName="test"
transitionEnterTimeout={500}
transitionLeaveTimeout={300}>
>
<div key="transition-group-content" >
You prefer <strong>{props.testScore}</strong>!
</div>
</CSSTransitionGroup>
<CSSTransitionGroup
className="container result"
transitionName="test"
transitionEnterTimeout={500}
transitionLeaveTimeout={300}>
> ^ ** Must be a typo here **
^ ** Remove this '>' typo solve my problem **
The root cause of this error 'Error React.Children.only expected to receive a single React element child' is that there are two '>' in the ending of the JSX code.这个错误'Error React.Children.only expected to receive a single React element child'的根本原因是JSX代码末尾有两个'>'。 Remove one of the '>' solved this issue.
删除其中一个“>”解决了这个问题。
I had the same error using CSS Transitions.我在使用 CSS Transitions 时遇到了同样的错误。 What worked for me is to use these React tags:
<></>
to wrap around the tags inside the css transition tags.对我有用的是使用这些 React 标签:
<></>
来包裹 css 转换标签内的标签。
Notice I have two tags inside TransitionGroup and CSSTransition tags like so:注意我在 TransitionGroup 和 CSSTransition 标签中有两个标签,如下所示:
<TransitionGroup>
<CSSTransition key={quotesArr[active]}>
<>
<p>{current.quote}</p>
<h2>{current.client}</h2>
</>
</CSSTransition>
</TransitionGroup>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.