React.Children的用法详解中,有哪些长尾词技巧和最佳实践?
- 内容介绍
- 文章标签
- 相关推荐
本文共计814个文字,预计阅读时间需要4分钟。
React.Children是顶层API之一,提供了处理this.props.children这个封闭数据结构的有用工具。this.props对象的其他属性与组件的属性一一对应,但有一个例外,那就是this.props.children属性。它表示组件中嵌套的其他子组件或元素。
React.Children是顶层API之一,为处理this.props.children这个封闭的数据结构提供了有用的工具。
this.props对象的属性与组件的属性一一对应,但是有一个例外,就是this.props.children属性。它表示组件的所有子节点。
1、React.Children.map
object React.Children.map(object children, function fn [, object context]) 使用方法: React.Children.map(this.props.children, function (child) { return <li>{child}</li>; }) 其他方法 this.props.children.forEach(function (child) { return <li>{child}</li> })
在每一个直接子级(包含在 children 参数中的)上调用 fn 函数,此函数中的 this 指向 上下文。如果 children 是一个内嵌的对象或者数组,它将被遍历:不会传入容器对象到 fn 中。如果 children 参数是 null 或者 undefined,那么返回 null 或者 undefined 而不是一个空对象。
本文共计814个文字,预计阅读时间需要4分钟。
React.Children是顶层API之一,提供了处理this.props.children这个封闭数据结构的有用工具。this.props对象的其他属性与组件的属性一一对应,但有一个例外,那就是this.props.children属性。它表示组件中嵌套的其他子组件或元素。
React.Children是顶层API之一,为处理this.props.children这个封闭的数据结构提供了有用的工具。
this.props对象的属性与组件的属性一一对应,但是有一个例外,就是this.props.children属性。它表示组件的所有子节点。
1、React.Children.map
object React.Children.map(object children, function fn [, object context]) 使用方法: React.Children.map(this.props.children, function (child) { return <li>{child}</li>; }) 其他方法 this.props.children.forEach(function (child) { return <li>{child}</li> })
在每一个直接子级(包含在 children 参数中的)上调用 fn 函数,此函数中的 this 指向 上下文。如果 children 是一个内嵌的对象或者数组,它将被遍历:不会传入容器对象到 fn 中。如果 children 参数是 null 或者 undefined,那么返回 null 或者 undefined 而不是一个空对象。

