简体   繁体   English

像span一样反应渲染数组吗?

[英]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: 它如何处理每个元素取决于它是什么:

  • React components are rendered using their render() method. React组件使用其render()方法进行render()
  • Strings are wrapped in <span> tags (due to funny behavior of text nodes in the DOM, I believe) Strings包装在<span>标记中(我相信由于DOM中文本节点的有趣行为)
  • A nested array is flattened (ie: [1,2,[3,4]] is treated like [1,2,3,4] ) 嵌套数组被展平(即[1,2,[3,4]]被视为[1,2,3,4]
  • Other primitive types are converted to strings using .toString() and then treated as above. 其他基本类型使用.toString()转换为字符串,然后按上述方式处理。
  • Other types will cause an error 其他类型会导致错误

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM