[英]How to render several div in render with a function inside
I can't figure this out, I tried to use in render but still failed, how to fix this problem?我想不通,我尝试在渲染中使用但仍然失败,如何解决这个问题?
render() {
return (
{
<div className="ABC">
<a>ABC</a>
</div>
<div className="DEF">
}
{*Some function create inner html content*}
{
</div>
}
I tried to add tag but it seems like still got the error...我试图添加标签,但似乎仍然有错误......
The render method can only return a single root node. render 方法只能返回单个根节点。 You can either wrap everything in a single element (such as a
<div>
), or use react fragments您可以将所有内容都包装在单个元素中(例如
<div>
),或者使用react 片段
A common pattern in React is for a component to return multiple elements.
React 中的一个常见模式是组件返回多个元素。 Fragments let you group a list of children without adding extra nodes to the DOM.
Fragments 使您可以对子项列表进行分组,而无需向 DOM 添加额外的节点。
render() {
return (
<React.Fragment>
<ChildA />
<ChildB />
<ChildC />
</React.Fragment>
);
}
You will also see the above written in short Syntax as:您还将看到上面用简短的语法编写的内容:
render() {
return (
<>
<ChildA />
<ChildB />
<ChildC />
</>
);
}
You need to have a single closing element for JSX.您需要为 JSX 设置一个结束元素。 This might help :
这可能有帮助:
function bar(){
return (
<div>
Some other content
</div>
);
function foo(){
return (
<React.Fragment>
<div>
Some Content in here
{ bar() }
</div
</React.Fragment>
);
render(){
return(
{ foo() }
)
} }
You will have to use a single div tag.您将不得不使用单个div标签。 You can do this by
你可以这样做
function foo(){
return (
<div> New content here </div>
);
render(){
return(
<div className="ABC">
<a>ABC</a>
{ foo() }
</div>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.