[React] Understand React.Children Utilities
The data contained in this.props.children is not always what you might expect. React provides React.children to allow for a more consistent development experience.
For example, you have an component:
class App extends React.Component {
render(){
return (
<Parent>
<div className="childA"></div>
<div className="childB"></div>
</Parent>
)
}
}
Inside parent component, you have two children.
So if you log out 'this.props.children', it should be an Array.
class Parent extends React.Component {
render(){
console.log(this.props.children) // Array
return null
}
But things happen when 'Parent' component has only one Child:
class App extends React.Component {
render(){
return (
<Parent>
<div className="childA"></div>
</Parent>
)
}
}
When you log out 'this.props.children', it become an Object. So if you call '.map' on an Object, it will throw error.
What you can do for this is using 'React.Children.map':
let items = React.Children.map(this.props.children, (child) => console.log(child));
Even there is only one Child inside parent component, it will still convert it into an Array not a Object.
Other ways to do this such as:
let items = React.Children.forEach(this.props.children, (child) => console.log(child));
let items = React.Children.toArray(this.props.children)
If you only want Object which means just one Child, you can use:
let items = React.Children.only(this.props.children)
But if 'this.props.children' contains multi children, then it will throw an error.
[React] Understand React.Children Utilities的更多相关文章
- react中的children使用方法
使用过vue的小伙伴都知道vue中有个slot,也就是插槽,作用就是占位,那么再react中可以使用children来替代 父组件 render(){ return( <div> < ...
- 重谈react优势——react技术栈回顾
react刚刚推出的时候,讲react优势搜索结果是几十页. 现在,react已经慢慢退火,该用用react技术栈的已经使用上,填过多少坑,加过多少班,血泪控诉也不下千文. 今天,再谈一遍react优 ...
- JavaScript 和 React,React用了大量语法糖,让JS编写更方便。
https://reactjs.org/docs/higher-order-components.htmlhttps://codepen.io/gaearon/pen/WooRWa?editors=0 ...
- [React] Handle React Suspense Errors with an Error Boundary
Error Boundaries are the way you handle errors with React, and Suspense embraces this completely. Le ...
- React 之React.createContext
使用Context,可以跨越组件进行数据传递 import React from 'react'; import ReactDOM from 'react-dom'; const ThemeConte ...
- react第十三单元(react路由-react路由的跳转以及路由信息) #课程目标
第十三单元(react路由-react路由的跳转以及路由信息) #课程目标 熟悉掌握路由的配置 熟悉掌握跳转路由的方式 熟悉掌握路由跳转传参的方式 可以根据对其的理解封装一个类似Vue的router- ...
- React学习笔记-1-什么是react,react环境搭建以及第一个react实例
什么是react?react的官方网站:https://facebook.github.io/react/下图这个就是就是react的标志,非常巧合的是他和我们的github的编辑器Atom非常相似. ...
- 小谈React、React Native、React Web
React有三个东西,React JS 前端Web框架,React Native 移动终端Hybrid框架,React Web是一个源码转换工具(React Native 转 Web,并之所以特别提出 ...
- React的React Native
React的React Native React无疑是今年最火的前端框架,github上的star直逼30,000,基于React的React Native的star也直逼20,000.有了React ...
随机推荐
- Vue router的query对象里的值的问题
在使用 $router.push() 时,如果使用了query,则可以在跳转后从query中获取到对应的参数.如果传的是字符串自然没问题,但是如果传的其他类型的数据,在跳转之后是正常的,而跳转之后再刷 ...
- 【2017"百度之星"程序设计大赛 - 初赛(B)】Chess
[链接]http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=776&pid=1001 [题意] 在这里写题意 [题 ...
- System and method for critical address space protection in a hypervisor environment
A system and method in one embodiment includes modules for detecting an access attempt to a critical ...
- 【MapReduce】经常使用计算模型具体解释
前一阵子參加炼数成金的MapReduce培训,培训中的作业样例比較有代表性,用于解释问题再好只是了. 有一本国外的有关MR的教材,比較有用.点此下载. 一.MapReduce应用场景 MR能解决什么问 ...
- Windows环境下ARM集成开发环境的搭建与使用
Windows环境下能够使用Eclipse IDE for C/C++ Developers来搭建ARM开发环境 本文地址:http://blog.csdn.net/u011833609/articl ...
- 13.constexpr
#include <iostream> using namespace std; //声明返回值为常量表达式 constexpr int get() { ; return num; } v ...
- OpenCV人脸检測(完整源代码+思路)
本博文IDE为vs2013 OpenCV2.49 话不多说,先看视频演示(20S演示): 例如以下: https://v.youku.com/v_show/id_XMjYzMzkxMTYyMA==.h ...
- 机房收费 & 廊院食堂
做机房收费系统时.常常想这个一般用户指的是谁?我当初以为是学生......可能是被数据库中的student带跑偏了...... 事实上把我们的系统联系一下实际,就会非常easy想到一般用户指的是谁的位 ...
- 以流的形式接收Http请求
IEnumerable<ReportObject> IHttpReceiveReport.OnPost(System.Web.HttpContextBase context) { //[{ ...
- Java核心技术 卷Ⅰ 基础知识(4)
第六章 接口与内部类 接口 特性 接口与抽象类 对象克隆 接口与回调 内部类 使用内部类访问对象状态 内部类的特殊语法规则 局部内部类 匿名内部类 静态内部类 代理 Class[] in=new Cl ...