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的更多相关文章

  1. react中的children使用方法

    使用过vue的小伙伴都知道vue中有个slot,也就是插槽,作用就是占位,那么再react中可以使用children来替代 父组件 render(){ return( <div> < ...

  2. 重谈react优势——react技术栈回顾

    react刚刚推出的时候,讲react优势搜索结果是几十页. 现在,react已经慢慢退火,该用用react技术栈的已经使用上,填过多少坑,加过多少班,血泪控诉也不下千文. 今天,再谈一遍react优 ...

  3. JavaScript 和 React,React用了大量语法糖,让JS编写更方便。

    https://reactjs.org/docs/higher-order-components.htmlhttps://codepen.io/gaearon/pen/WooRWa?editors=0 ...

  4. [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 ...

  5. React 之React.createContext

    使用Context,可以跨越组件进行数据传递 import React from 'react'; import ReactDOM from 'react-dom'; const ThemeConte ...

  6. react第十三单元(react路由-react路由的跳转以及路由信息) #课程目标

    第十三单元(react路由-react路由的跳转以及路由信息) #课程目标 熟悉掌握路由的配置 熟悉掌握跳转路由的方式 熟悉掌握路由跳转传参的方式 可以根据对其的理解封装一个类似Vue的router- ...

  7. React学习笔记-1-什么是react,react环境搭建以及第一个react实例

    什么是react?react的官方网站:https://facebook.github.io/react/下图这个就是就是react的标志,非常巧合的是他和我们的github的编辑器Atom非常相似. ...

  8. 小谈React、React Native、React Web

    React有三个东西,React JS 前端Web框架,React Native 移动终端Hybrid框架,React Web是一个源码转换工具(React Native 转 Web,并之所以特别提出 ...

  9. React的React Native

    React的React Native React无疑是今年最火的前端框架,github上的star直逼30,000,基于React的React Native的star也直逼20,000.有了React ...

随机推荐

  1. 一个理性战胜感性的成功案例:P2P投资和活期理财,纠结中提炼出来的1个数学问题

    我经常是投资了P2P,然后用钱,因而损失了一部分收益. 这是一个让我纠结的问题,为了解决这个问题,我不再凭感觉,而是从现实情况,提炼出来1个数学题,解答我的疑惑. 这是一个理性战胜感性的成功案例~ P ...

  2. ListCtrl添加右键菜单(在对话框类中)

    在对话框类中如何添加NM_RCLICK消息: ListCtrl控件右键单击选择属性 在右侧属性栏中选择控件事件 在控件事件中找到NM_RCLICK并添加 完成,写代码

  3. 洛谷——U10783 名字被和谐了

    https://www.luogu.org/problem/show?pid=U10783 题目背景 众所周知,我们称g是a的约数,当且仅当g是正数且a mod g = 0. 众所周知,若g既是a的约 ...

  4. Java String对象的经典问题

     先来看一个样例,代码例如以下:  public class Test {       public static void main(String[] args) {           Strin ...

  5. 6.CPP风格数组

    //数组初始化 1 #include <iostream> using namespace std; void main() { //C int a[5] = { 1,2,3,4,5 }; ...

  6. php编译参数注释

    1. 指定安装路径 --prefix=PREFIX 2. 指定运行用户 --with-fpm-user=nginx 3. 指定运行组 --with-fpm-group=nginx 3.与'--pref ...

  7. Sql Server 2014完全卸载

    经历过好多次Sql server的安装与卸载,有时发现自己卸载的费时费力,单纯地卸载个软件就要吐血了,那么现在我觉得是时候整理一下了. 1.在运行中输入services.msc,然后找到所有跟Sql ...

  8. 怎样让IE支持自己定义协议

    浏览QQ空间的时候发现,仅仅要在IE地址中输入象一下这样的形式的地址. tencent://Message/?Uin=251464630&websiteName=qzone.qq.com&am ...

  9. Android实践 -- Android蓝牙设置连接

    使用Android Bluetooth APIs将设备通过蓝牙连接并通信,设置蓝牙,查找蓝牙设备,配对蓝牙设备 连接并传输数据,以下是Android系统提供的蓝牙相关的类和接口 BluetoothAd ...

  10. Day2:字符串常用方法

    字符串常用方法 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan name = "my \tname is ...