[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 ...
随机推荐
- 配置TL-WVR45G企业路由动态地址
1.打开浏览器,在地址栏输入http://192.168.1.1. 2.输入默认用户名密码:admin,登录. 3.[基本设置]->[lan设置]->[lan设置] ip地址改成:192 ...
- struts2怎么返回一个字符串给jsp?(使用json)
我们都知道使用servlet时可以直接用PrintWriter对象的print方法来向页面传送一些字符串(可以是html标签和内容),然后在用RequestDispatcher来转向网页 虽Strut ...
- 2016届 阿里巴巴校招研发project师C/C++笔试题--2015.08.23
选择题牛客网地址题目1:http://www.nowcoder.com/test/255234/summary. 题目2:http://www.nowcoder.com/test/262758/sum ...
- LeetCode -- 最大连续乘积子序列
问题描写叙述: 给定数组,找出连续乘积最大值的子序列.比如 0,-1,-3.-2.则最大连续乘积为6= (-3) * (-2) 实现思路此题与最大连续和的子序列问题相似,也可通过找到递推公式然后用DP ...
- 怎样给UINavigationBar加入button?
Mads Mobæk:给UINavigationBar加入button的演示样例代码 1 2 3 4 5 6 7 8 UIBarButtonItem *rightButton = [[UIBarBut ...
- 关于WMware Workstation出现 “”该虚拟机似乎正在使用中“”解决办法
如图 直接到配置文件D:\centos1\这个目录下 将vmdk.lck文化删除 然后重新打开虚拟机便可进入 问题解决
- 安装oracle常见问题分析
1,之前已经安装过软件,卸载不完全 /tmp/.oracle /tmp/ora* /var/tmp/ora* /etc/ora* 2,安装介质权限或者本身就有问题 3,oracle用户 $/HOME/ ...
- mysql安装遇到的坑
安装mysql的三步: mysqld --initialize-insecure mysqld -install net start mysql 中间遇到了坑, 看这篇文章完美的解决了,记录一下 .以 ...
- 关于IDEA无法完整显示项目文件结构
今天发现一个奇怪的问题,就是我从本地导入了文件,明明已经成功了,但是在我的项目结构里面就是不显示,然而点击目录, 又能打开相应的文件,如图所示: 其实这个打开的只是包文件,只需要如下图操作即可:
- python3之开发环境PyCharm配置
1. 安装PyCharm(安装时注意选择python),地址: https://www.jetbrains.com/pycharm/ 2. 安装python 地址: https://www.pytho ...