Compound component gives more rendering control to the user. The functionality of the component stays intact while how it looks and the order of the children can be changed at will. We get this functionality by using the special React.Children.map function to map over the children given to our <Toggle/> component. We map over the children to pass the on state as a prop to its children. We move the visual pieces of the component out into function components and add them as static properties to <Toggle/>.

User has free control how On / Off / Button shows on the page:

    <Toggle onToggle={(on) => console.log("Toggle", on)}>
<Toggle.On>
Switch is On!
</Toggle.On>
<Toggle.Button />
<Toggle.Off>
Switch is Off!
</Toggle.Off>
</Toggle>;

Toggle.On, Toggle.Off and Toggle.Button is private components for Toggle:

class Toggle extends React.Component {

  // An empty function
static defaultProps = {onToggle: () => {}};
static On = ToggleOn;
static Off = ToggleOff;
static Button = ToggleButton; .... }

All those components have props, which user doesn't need to care about, those props should be passed down fromt the parent component: Toggle:

const ToggleOn = ({on, children}) => {
if(on) {
return (<div>{children}</div>)
} else {
return null;
}
};
const ToggleOff = ({on, children}) => {
if(on) {
return null;
} else {
return (<div>{children}</div>);
}
};
const ToggleButton = ({on, toggle, ...props}) => (
<Switch on={on} onClick={toggle} {...props} />
);

This can be done by using React.Children.map and React.cloneElement:

  render() {
const {on} = this.state;
const children = React.Children.map(
this.props.children,
(child) => React.cloneElement(child, {
on: this.state.on,
toggle: this.toggle
})
);
return (
<div> {children} </div>
)
}

[React] Write Compound Components的更多相关文章

  1. [React] Prevent Unnecessary Rerenders of Compound Components using React Context

    Due to the way that React Context Providers work, our current implementation re-renders all our comp ...

  2. [React] Make Compound React Components Flexible

    Our current compound component implementation is great, but it's limited in that users cannot render ...

  3. [React] Validate Compound Component Context Consumers

    If someone uses one of our compound components outside the React.createContext <ToggleContext.Pro ...

  4. 【转】Facebook React 和 Web Components(Polymer)对比优势和劣势

    原文转自:http://segmentfault.com/blog/nightire/1190000000753400 译者前言 这是一篇来自 StackOverflow 的问答,提问的人认为 Rea ...

  5. Facebook React 和 Web Components(Polymer)对比优势和劣势

    目录结构 译者前言 Native vs. Compiled 原生语言对决预编译语言 Internal vs. External DSLs 内部与外部 DSLs 的对决 Types of DSLs - ...

  6. [Angular] Write Compound Components with Angular’s ContentChild

    Allow the user to control the view of the toggle component. Break the toggle component up into multi ...

  7. [React Fundamentals] Composable Components

    To make more composable React components, you can define common APIs for similar component types. im ...

  8. [React] Higher Order Components (replaces Mixins)

    Higher order components will allow you to apply behaviors to multiple React components. So the idea ...

  9. [React] React Router: Named Components

    In this lesson we'll learn how to render multiple component children from a single route. Define a n ...

随机推荐

  1. MDNS的漏洞报告——mdns的最大问题是允许广域网的mdns单播查询,这会暴露设备信息,或者被利用用于dns放大攻击

    Vulnerability Note VU#550620 Multicast DNS (mDNS) implementations may respond to unicast queries ori ...

  2. 从Git里拉取远程的所有分支

    从Git里拉取远程的所有分支 git branch -r | grep -v '\->' | while read remote; do git branch --track "${r ...

  3. BZOJ 4552 排序 Heoi2016

    记得当年省选的时候 这道题连暴力都没写对(尴尬ing) (当年天真的认为sort是左闭右闭的hhhhhh) 思路: 首先 二分答案 线段树 首先二分答案,然后需要知道进行m次排序后p位置上的数字是否大 ...

  4. jqueryValidator自定义校验规则的一种方式(不覆盖源码)

    1.封装自定义验证方法-validate-methods.js /***************************************************************** j ...

  5. Spring深入浅出(一)IOC的基本知识

    Spring前言 Spring是一个企业级开发框架,为解决企业级项目开发过于复杂而创建的,框架的主要优势之一就是分层架构,允许开发者自主选择组件. Spring的两大核心机制是IOC(控制反转)和AO ...

  6. 08:Challenge 1

    总时间限制:  10000ms 单个测试点时间限制:  1000ms 内存限制:  262144kB 描述 给一个长为N的数列,有M次操作,每次操作是以下两种之一: (1)修改数列中的一个数 (2)求 ...

  7. Xor Sum 2(位运算)

    D - Xor Sum 2 Time limit : 2sec / Memory limit : 1024MB Score : 500 points Problem Statement There i ...

  8. Passpoint R1

    Passpoint R1 自从 Android 6.0 支持从网络下载包含配置文件和凭据信息的特殊文件来配置 Passpoint R1(第 1 版)凭据,Android 就一直支持 Passpoint ...

  9. C#中使用Dictionary实现Map数据结构——VC编程网

    转载自: http://blog.51cto.com/psnx168 在VC中使用过CMap以及在Java中使用过Map的朋友应该很熟悉,使用Map可以方便实现基于键值对数据的处理,在C#中,你就需要 ...

  10. iptables指南

    在了解iptables之前我们先了解一下 防火墙 的概念防火墙是由Check Point创立者Gil Shwed于1993年发明并引入国际互联网,防火墙也是一种位于内部网络与外部网络之间的网络安全系统 ...