码迷,mamicode.com
首页 > 其他好文 > 详细

为什么用React.Children.map(props.children, () => )而不是props.children.map(() => )

时间:2019-01-16 13:17:57      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:map   div   数组   react   ops   nbsp   array   bsp   代码   

不能保证props.children将是一个数组。

以此代码为例,

<Parent>
  <h1>Welcome.</h1>
</Parent>

 

如果我们尝试使用props.children.map它来映射孩子,父母内部会抛出错误,因为它props.children是一个对象而不是一个数组。

props.children如果有多个子元素(如此),则React只会生成一个数组

<Parent>
  <h1>Welcome.</h1>
  <h2>props.children will now be an array</h2>
</Parent>

 

这就是为什么你想要赞成,React.Children.map因为它的实现考虑到props.children可能是一个数组或一个对象。

 

为什么用React.Children.map(props.children, () => )而不是props.children.map(() => )

标签:map   div   数组   react   ops   nbsp   array   bsp   代码   

原文地址:https://www.cnblogs.com/passkey/p/10276314.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!