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. windows 快捷调用

    win + x:系统常用管理工具: win + r,或者按下 windows 键,在输入框中输入: services.msc:服务管理: diskmgmt.msc:磁盘管理: devmgmt.msc: ...

  2. rest_framework-权限-总结完结篇

    #权限#创建一个权限类 在view添加列表 class MyPermission(object): #message 表示权限决绝时返回的数据 message = "必须是SVIP" ...

  3. mysql if使用实例

    //DELIMITER DROP PROCEDURE if EXISTS createAmountCount; create PROCEDURE createAmountCount() BEGIN D ...

  4. 基于JavaSwing的例子-非连接数据库

    项目结构: Constant.java package com.mstf.test; import java.io.Serializable; public class Constant implem ...

  5. append生成新变量的时候,没有如预期(It's a feature,not a bug?)

    这是我在写一个项目中,遇到的一个golang的feature,如代码所示,我在for循环里,每次用append生成一个新的数组,(当然我以前一直以为可以这样,直到我在stackoverflow上发现不 ...

  6. Android chromium 1

    For Developers‎ > ‎Design Documents‎ > ‎ Java Resources on Android Overview Chrome for Android ...

  7. 冒泡排序php

    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> <b ...

  8. phpstorm10安装并汉化

    一.下载phpstorm 下载地址:https://pan.baidu.com/s/1R64ZROVP1ljGbYfCwWjwxA 二.一直点击下一步安装即可 注意:第3步的时候选择一下支持的后缀 三 ...

  9. 对比学习:《深度学习之Pytorch》《PyTorch深度学习实战》+代码

    PyTorch是一个基于Python的深度学习平台,该平台简单易用上手快,从计算机视觉.自然语言处理再到强化学习,PyTorch的功能强大,支持PyTorch的工具包有用于自然语言处理的Allen N ...

  10. Python中的引用计数法

    目录 引用计数法 增量操作 计数器溢出的问题 减量操作 终结器 插入计数处理 引用计数法 增量操作 如果对象的引用数量增加,就在该对象的计数器上进行增量操作.在实际中它是由宏Py_INCREF() 执 ...