[React] Compound Component (React.Children.map & React.cloneElement)
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)的更多相关文章
- react 也就这么回事 01 —— React 元素的创建和渲染
React 是一个用于构建用户界面的 JavaScript 库 它包括两个库:react.js 和 react-dom.js react.js:React 的核心库,提供了 React.js 的核心功 ...
- React 的高级用法(Children、Component、createElement、cloneElement)
React.Children props.children 代表了所有的子节点. React.Children 用于处理 props.children 的 提供了几个方法 ( map ,foreach ...
- React源码解析之React.Children.map()(五)
一,React.Children是什么? 是为了处理this.props.children(this.props.children表示所有组件的子节点)这个属性提供的工具,是顶层的api之一 二,Re ...
- React中props.children和React.Children的区别
在React中,当涉及组件嵌套,在父组件中使用props.children把所有子组件显示出来.如下: function ParentComponent(props){ return ( <di ...
- Ch03 React/JSX/Component 簡介
Facebook 本身有提供 Test Utilities,但由于不够好用,所以目前主流开发社群比较倾向使用 Airbnb 团队开发的 enzyme,其可以与市面上常见的测试工具(Mocha.Karm ...
- React的this.props.children
this.props用来获取组件从外部传入的属性,但是this.props.children比较特殊,它是由React给添加上的,表示组件的所有子节点.this.props.children可以用来读 ...
- react中对props.children进行操作
之前写的更多的时候是直接使用简写 {props.children} 这样就可以了,但是当有时候需要对传入的子组件进行一些操作的时候需要用到React.Children方法 {React.Childre ...
- react第十单元(children的深入用法-React.Children对象上的方法)
第十单元(children的深入用法-React.Children对象上的方法) #课程目标 理解什么是children 掌握React.Children对象上的方法 #知识点 什么是children ...
- react slot component with args
react slot component with args how to pass args to react props child component https://codesandbox.i ...
随机推荐
- 洛谷1005 【NOIP2007】矩阵取数游戏
问题描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij均为非负整数.游戏规则如下: 1.每次取数时须从每行各取走一个元素,共n个.m次后取完矩阵所有元素: 2. ...
- ArchLinux dwm的安装和配置
dwm官网:https://dwm.suckless.org/ dwm是一个简洁的平铺式窗口管理器 配置简单,使用便捷,没有多少依赖,占用内存非常小 总之dwm正合口味 安装方法 首先在官网下载dwm ...
- CF 986C AND Graph(建模+DFS)
#include<stdio.h> ],v[]; ],n,al; void dfs(int x){ if(v[x])return; v[x]=; if(ex[x])dfs(al^x); ; ...
- Git学习笔记 2,GitHub常用命令
廖雪峰Git教程 莫烦Git教程 莫烦Git视频教程 文件三个状态,add之后从工作区(原始状态)到暂存区,commit之后从暂存区到版本库 工作区 暂存区 版本库 unstage stage mas ...
- Zookeeper入门:基本概念、5项配置、启动
起源 最早接触Zookeeper,是在学习Hadoop权威指南这本书的时候,印象中是Hadoop项目的一个子工程. 最近,项目中需要用到"分布式锁". 之前,在 ...
- windows linux 双系统默认启动windows 的几种方法
装了双系统后,在开机时总会有想让一个系统默认启动的时候,一般安装完Ubuntu和XP双系统后,开机时默认的是启动Ubuntu系统,可是当想让XP作为默认启动的系统时怎么办呢? 在早期的Ubuntu系统 ...
- CentOS 搭建 Mysql MMM 高可用架构
环境 CentOS Mysql 5.1 前提 安装了EPEL,详细安装步骤请參照 http://blog.csdn.net/robinsonmhj/article/details/36184863 机 ...
- DevExpress控件的安装及画图控件的使用
近期须要绘制纵断面图,而AE自带的又不是非常好,查找资料后使用DevExpress控件中的画图控件实现了纵断面的绘制.Dev控件是须要付费的.这里我们使用破解版的哈. 安装包及破解文件上传至我的网盘了 ...
- BZOJ 3262 cdq分治 OR 树套树
注意判断 三个条件都一样的-- (CDQ分治 其实并不是很难理解 只是想不到--) CDQ分治: //By SiriusRen #include <cstdio> #include < ...
- HTML5新增的一些特性
HTML现在已经不是SGML的子集,主要是关于图像,位置,储存,多任务等功能的增加. .绘画canvas: .用于媒介回放的video: Ogg是带有Theora视频编码和Vorbis音频编码的文件: ...