ReactJs中的this.props.children总结
来源:互联网 发布:检测到usb端口潮湿 编辑:程序博客网 时间:2024/06/07 07:00
this.props 对象的属性与组件的属性一一对应,但是有一个例外,就是 this.props.children 属性。它表示组件的所有子节点
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
上面代码的 NoteList 组件有两个 span 子节点,它们都可以通过 this.props.children 读取,运行结果如下。
- hello
- world
这里需要注意, this.props.children 的值有三种可能:如果当前组件没有子节点,它就是 undefined ;如果有一个子节点,数据类型是 object ;如果有多个子节点,数据类型就是 array 。所以,处理 this.props.children 的时候要小心。
React 提供一个工具方法 React.Children 来处理 this.props.children 。我们可以用 React.Children.map 来遍历子节点,而不用担心 this.props.children 的数据类型是 undefined 还是 object。更多的 React.Children 的方法,请参考官方文档 https://facebook.github.io/react/docs/top-level-api.html#react.children 。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
输出结果为:
思考思考—-0
瞬间即逝—-1
阿达瓦得—-2
map遍历数组
阅读全文
0 0
- ReactJs中的this.props.children总结
- ReactJs中的this.props.children总结
- 2、Reactjs中的属性(this.props)
- Reactjs中的属性(this.props)
- Reactjs中的属性(this.props)
- react this.props.children
- reactjs新手篇this.props
- react native -- this.props.children
- React.js--this.props.children
- React list this.props.children
- react教程之this.props.children
- react教程序之this.props.children
- React学习(五)this.props.children
- react教程之this.props.children
- 第四节, this.props.children 获取子元素并插入
- react demo8 (设置组件自身属性this.props.children)
- React学习笔记(2)-React.createClass、this.props.children
- reactjs(四)props的作用
- Aspose.Cells 12月新版17.12发布 | 附下载
- 树莓派
- Android下打印调试堆栈方法
- 鞍点计算
- 基于Graphics画图
- ReactJs中的this.props.children总结
- 第一阶段-入门详细图文讲解tensorflow1.4 API-tf.nn.conv2d
- D3结合Drupal8的初次尝试
- 代码行数统计的Java和Python实现
- etl,bi工具下载
- HTTP 错误 500.19,错误代码0x800700b7
- 【Spring+SpringMVC+MyBatis深入学习及搭建】16.SpringMVC注解开发(高级篇)
- 应用去除标题栏以及全屏的方法以及失败的处理
- 趟过spark的坑