[英]React rendering arrays like spans?
I got this inside a component render
function return statement: 我在组件
render
函数return语句中得到了这个:
<div className="well">
{this.props.someArray}
</div>
Let's say someArray = [1,2]
, what I've noticed is React is actually rendering each of the element tags like <span>
elements. 假设
someArray = [1,2]
,我注意到React实际上是渲染每个元素标签,例如<span>
元素。 I thought I was going to get an error. 我以为我会出错。
Is this recommended? 推荐这个吗? Are there any guidelines as to what React does when you pass a raw data structure?
在传递原始数据结构时,React有什么指导方针吗? I haven't seen any material on this.
我还没有看到任何材料。
When React encounters an element that's an array, it renders each element in that array. 当React遇到一个数组元素时,它将渲染该数组中的每个元素。 How it handles each element depends on what it is:
它如何处理每个元素取决于它是什么:
render()
method. render()
方法进行render()
。 Strings
are wrapped in <span>
tags (due to funny behavior of text nodes in the DOM, I believe) Strings
包装在<span>
标记中(我相信由于DOM中文本节点的有趣行为) [1,2,[3,4]]
is treated like [1,2,3,4]
) [1,2,[3,4]]
被视为[1,2,3,4]
) .toString()
and then treated as above. .toString()
转换为字符串,然后按上述方式处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.