[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 ...
随机推荐
- Java Servlet学习笔记(四)Servlet客户端Http请求
Servlet 客户端 HTTP 请求 当浏览器请求网页时,它会向 Web 服务器发送特定信息,这些信息不能被直接读取,因为这些信息是作为 HTTP 请求的头的一部分进行传输的.您可以查看 HTTP ...
- 51NOD——T 1079 中国剩余定理
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1079 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难 ...
- 【2017 Multi-University Training Contest - Team 10 】Monkeys
[链接]点击打开链接 [题意] 给你一棵n节点的树,现在让你放k个猴子,可以删边,问最少可以剩余几条边,放k个猴子,满足任意一个猴 子至少与一只猴子相连.2<=k<=n<=1e5 [ ...
- C# mongodb帮助类
这是在C#连接MongoDB的帮助类,所使用的驱动是在Vs2015的Nuget管理器中下载的mongodb驱动. 下载第一个,会自动下载下面的两个,不要删除. 在配置文件中配置连接字符串connStr ...
- 为easyUI的dataGrid加入自己的查询框
dataGrid作为easyUI的一个核心组件,其功能上是非常强大的. 可是外观上似乎就有点差强人意了,首先说一下我对dataGrid外观的2点感受 1.图标不好看,且尺寸非常小(16x16)-- 关 ...
- 基于ContentObserver来动态取消或加入屏幕超时任务
前面也说了.ContentObserver能够来监控数据库里某一项数据的变化,当然也能够同一时候监控多个数据项的变化. 笔者在项目中须要改动到屏幕超时的需求,比方在车载业务中,倒车事件发生的时候,是不 ...
- web.config访问走代理的配置
<system.net> <defaultProxy> <proxy bypassonlocal="False" usesystemd ...
- 【例题 4-1 UVA - 1339】 Ancient Cipher
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 位置其实都没关系了. 只要每个字母都有对应的字母,它们的数量相同就可以了. 求出每种字母的数量. 排序之后. 肯定是要一一对应的. ...
- storm集群操作指南
目录 storm集群操作指南 一.storm伪分布式安装 (一)环境准备 (二)安装zookeeper (三)安装storm (四)运行程序 二.storm集群安装 (一)下载storm并解压 (二) ...
- IPv4与IPv6数据报格式详解
摘要: 本文给出IPv4与IPv6数据报格式示意图,并整理了各个字段含义,最后对比IPv4与IPv6数据报格式的区别. 一.IPv4数据报 图1 IPv4数据报格式版本号(version) 不同的IP ...