Imaging you are building a Tabs component.

If looks like:

<Tabs>
<TabList>
<Tab> one </Tab>
<Tab isDisabled> two </Tab>
<Tab> three </Tab>
</TabList> <TabPanels>
<TabPanel>
content one
</TabPanel>
<TabPanel>
content two
<TabPanel>
</TabPanel>
<TabPanel>
content three
</TabPanel>
</TabPanels>
</Tabs>

You want to know which tab is clicked (actived). But you don't want this actived tab state be exported to our app, you want it been kept to Tabs component.

For example in Tabs component, there is a state called 'activedIndex':

class Tab extends React.Component {
state = {
activedIndex: 0
} render() {
return (
    {this.props.children}
  );
}
}

You want to pass down to TabList and TabPanels componet. And also TabList and TabPanels may receive different props depends on usecases.

You can use 'React.Children.map' to map over each direct child of the componet (in our case is TabPanels and TabList).

 React.Children.map(this.props.children, (child, index) => {

To pass down the new props, we can use 'React.cloneElement', which need the old props if any, but merge with new props we pass in.

return React.cloneElement(child, {
activeIndex: this.state.activeIndex
})

Code:

class Tab extends React.Component {
state = {
activedIndex:
} render() {
return (
React.Children.map(this.props.children, (child, index) => {
if (child.type === TabPanels) {
return React.cloneElement(child, {
activeIndex: this.state.activeIndex
})
} else if(child.type === TabList) {
return React.cloneElement(child, {
activeIndex: this.state.activeIndex,
isActivate: index === this.state.activeIndex
})
} else {
return child
}
})
)
}
}

[React] Compound Component (React.Children.map & React.cloneElement)的更多相关文章

  1. react 也就这么回事 01 —— React 元素的创建和渲染

    React 是一个用于构建用户界面的 JavaScript 库 它包括两个库:react.js 和 react-dom.js react.js:React 的核心库,提供了 React.js 的核心功 ...

  2. React 的高级用法(Children、Component、createElement、cloneElement)

    React.Children props.children 代表了所有的子节点. React.Children 用于处理 props.children 的 提供了几个方法 ( map ,foreach ...

  3. React源码解析之React.Children.map()(五)

    一,React.Children是什么? 是为了处理this.props.children(this.props.children表示所有组件的子节点)这个属性提供的工具,是顶层的api之一 二,Re ...

  4. React中props.children和React.Children的区别

    在React中,当涉及组件嵌套,在父组件中使用props.children把所有子组件显示出来.如下: function ParentComponent(props){ return ( <di ...

  5. Ch03 React/JSX/Component 簡介

    Facebook 本身有提供 Test Utilities,但由于不够好用,所以目前主流开发社群比较倾向使用 Airbnb 团队开发的 enzyme,其可以与市面上常见的测试工具(Mocha.Karm ...

  6. React的this.props.children

    this.props用来获取组件从外部传入的属性,但是this.props.children比较特殊,它是由React给添加上的,表示组件的所有子节点.this.props.children可以用来读 ...

  7. react中对props.children进行操作

    之前写的更多的时候是直接使用简写 {props.children} 这样就可以了,但是当有时候需要对传入的子组件进行一些操作的时候需要用到React.Children方法 {React.Childre ...

  8. react第十单元(children的深入用法-React.Children对象上的方法)

    第十单元(children的深入用法-React.Children对象上的方法) #课程目标 理解什么是children 掌握React.Children对象上的方法 #知识点 什么是children ...

  9. react slot component with args

    react slot component with args how to pass args to react props child component https://codesandbox.i ...

随机推荐

  1. vue使用axios中 this 指向问题

    1.解决办法 在vue中使用axios做网络请求的时候,会遇到this不指向vue,而为undefined,可以使用箭头函数"=>"来解决.如下: methods: { lo ...

  2. ArcGIS api for javascript——地图配置-增加一个调试控制台<

    描述 该示例展示了在应用中如何包含一个Dojo调试控制台.可以写信息到控制台来记录发生的事件和应用运行时设置的属性.当调试Internet Explorer浏览器的错误时这是极为有帮助的.(对Fire ...

  3. cocos2d-js导弹跟踪算法(一边追着目标移动一边旋转角度)

    跟踪导弹 function(targetPosition){ // 让物体朝目标移动的方法 ; var targetPoint = targetPosition; var thisPoint = cc ...

  4. Linux_Oracle10 下载安装

    Linux_Oracle10 安装文档及oracle下载地址 http://download.csdn.net/detail/u012750578/8177405 一.启动数据库服务 [root@hq ...

  5. codeforces 558E A Simple Task 线段树

    题目链接 题意较为简单. 思路: 由于仅仅有26个字母,所以用26棵线段树维护就好了,比較easy. #include <iostream> #include <string> ...

  6. html2canvas截取页面

    1.下载html2canvas.js 2.引入 3.修改html2canvas支持远程图片处理 function ImageContainer(src, cors) { this.src = src; ...

  7. 公司采购 流程flowable例子

    Name: Flowable BPMN 2.0 designer Location: http://flowable.org/designer/update/ 业务描述:1. 公司采购,因为办公用品价 ...

  8. spring security源码分析心得

    看了半天的文档及源码,终于理出了spring-security的一些总体思路,spring security主要分认证(authentication)和授权(authority). 1.认证authe ...

  9. ps切图时常用的操作与快捷键

    一:两种切片方法 第一种: 1.使用切片工具划分好你要切的模块 2.点击'存储为web所有格式',在存储之前可以修改图片的品质来改变文件的大小. 3.在存储时切片有三种选择方式,按照自己的需要选择. ...

  10. 学习推荐《Python神经网络编程》中文版PDF+英文版PDF+源代码

    推荐非常适合入门神经网络编程的一本书<Python神经网络编程>,主要是三部分: 介绍神经网络的基本原理和知识:用Python写一个神经网络训练识别手写数字:对识别手写数字的程序的一些优化 ...