[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 ...
随机推荐
- unity 美术注意事项
有时候美术的一个不小心,就会给程序徒增极大的工作量,所以在项目开始之前是有必要和美术沟通一下,来规范一些东西, 1.将单体模型的轴心置中. 2.模型有父物体时,子物体应相对于父物体的(0,0,0)位置 ...
- 自己主动化脚本ssh以及telnet发送命令并退出(windows和linux都适用)
须要安装putty,用到的命令是plink: PuTTY Link: command-line connection utility Unidentified build, Jun 23 2015 1 ...
- UVA 10515 - Powers Et Al.(数论)
UVA 10515 - Powers Et Al. 题目链接 题意:求出m^n最后一位数 思路:因为m和n都非常大,直接算肯定是不行的,非常easy想到取最后一位来算,然后又非常easy想到最后一位不 ...
- 两天学会DirectX 3D之入门
环境配置以及背景知识 环境 Windows 8.1 64bit VS2013 Microsoft DirectX SDK (June 2010) NVDIA Geforce GT755 环境的配置參考 ...
- linux和unix的对照
在之前的博客中说到,linux是一个单一内核的操作系统,但它与传统的单一内核UNIX操作系统不同. 在普通单一内核系统中,全部内核代码都是被静态编译和链接的. 而在linux中,能够动态的装入和卸载内 ...
- Cordic 算法入门
三角函数的计算是个复杂的主题,有计算机之前,人们通常通过查找三角函数表来计算任意角度的三角函数的值.这种表格在人们刚刚产生三角函数的概念的时候就已经有了,它们通常是通过从已知值(比如sin(π/2)= ...
- [UOJ#334][NOIP2017]列队 平衡树/线段树/树状数组
题目链接 题意不说了,一辈子也忘不掉 解法1.平衡树 这题就是平衡树裸题,每一行开一棵维护前 \(m-1\) 个,最后一列单独维护,因为很多人没有用到,所以平衡树每个节点是一个区间(pair),分裂时 ...
- docker 笔记1
如果想要删除所有container的话再加一个指令: docker stop $(docker ps -a -q) 如果想要删除所有container的话再加一个指令: docker rm $(doc ...
- 记录一下sql两个表关联的查询使用方法
SELECT * FROM t_yymp_user_info where user_id = (select b.user_id from t_yymp_auth_role as a,t_yymp_a ...
- 分享一下js正则中惰性与贪婪
首先引入一个介绍比较详细的网站 http://www.cnblogs.com/yuaima/p/5258513.html http://www.jb51.net/article/31491.htm 接 ...