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. POJ2104 K-th Number(线段树,二分,vector)

    题意 不带修改区间第k小.(n<=100000) 题解 建立线段数和vector数组(vector为当前区间排列之后的序列)(归并) 然后对于每一个询问二分答案. 问题就转化为区间有多少数小于等 ...

  2. 05002_Linux的安装

    1.虚拟机VMware workstation的安装 (1)下载链接:VMware workstation下载 密码:52wt: (2)双击VMware-workstation-full-10.0.2 ...

  3. ECNUOJ 2615 会议安排

    会议安排 Time Limit:1000MS Memory Limit:65536KB Total Submit:451 Accepted:102 Description 科研人员与相关领域的国内外同 ...

  4. 三:redis的List类型相关操作

    </pre><pre name="code" class="php" style="font-size: 14px;"&g ...

  5. css footer not displaying at the bottom of the page

    https://stackoverflow.com/questions/15960290/css-footer-not-displaying-at-the-bottom-of-the-page The ...

  6. 128.C++文件操作小结

    打开后缀参数 #include <fstream> #include <iostream> using namespace std; //文本读写 //文件写入 void ma ...

  7. SQL调用Webservices

    IF NOT object_id('proc_CallWebServices') IS NULL DROP PROCEDURE proc_CallWebServices GO CREATE PROCE ...

  8. Cisco交换机解决网络蠕虫病毒入侵问题

    Cisco交换机解决网络蠕虫病毒入侵问题          今年来网络蠕虫泛滥给ISP和企业都造成了巨大损失,截至目前已发现近百万种病毒及木马.受感染的网络基础设施遭到破坏,以Sql Slammer为 ...

  9. Linux下大型容量件的切割与合并

    Linux下大型容量件的切割与合并 当前目录下有sp4.exe文件容量129M我用64的U盘把spe.exe考到另一台机器上#split -b 60m sp4.exe-b, --bytes=SIZE  ...

  10. jasperreport 追加新报表(2)

    用ireport做好模版后,如果要新加一个打印页,如果是新手,直接修改模版应该是理想情况, 可是什么数据源 feild,parameter,var,subreport ,还有路径, 真的可以让一个人疯 ...