[React] Write Compound Components
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的更多相关文章
- [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 ...
- [React] Make Compound React Components Flexible
Our current compound component implementation is great, but it's limited in that users cannot render ...
- [React] Validate Compound Component Context Consumers
If someone uses one of our compound components outside the React.createContext <ToggleContext.Pro ...
- 【转】Facebook React 和 Web Components(Polymer)对比优势和劣势
原文转自:http://segmentfault.com/blog/nightire/1190000000753400 译者前言 这是一篇来自 StackOverflow 的问答,提问的人认为 Rea ...
- Facebook React 和 Web Components(Polymer)对比优势和劣势
目录结构 译者前言 Native vs. Compiled 原生语言对决预编译语言 Internal vs. External DSLs 内部与外部 DSLs 的对决 Types of DSLs - ...
- [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 ...
- [React Fundamentals] Composable Components
To make more composable React components, you can define common APIs for similar component types. im ...
- [React] Higher Order Components (replaces Mixins)
Higher order components will allow you to apply behaviors to multiple React components. So the idea ...
- [React] React Router: Named Components
In this lesson we'll learn how to render multiple component children from a single route. Define a n ...
随机推荐
- Android ListView动画实现方法
在Android中listview是最经常使用的控件之中的一个,可是有时候我们会认为千篇一律的listview看起来过于单调,于是就产生了listView动画,listview载入了动画会让用户体验更 ...
- poj_2187凸包,暴力解法
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...
- 用DOM动态控制表格
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- jni java与c++交互返回三维数组jobjectArray
1.在java里创建一个类,在类里添加静态方法调用c++的类库dll,在java里创建要返回数组的函数.在java的main方法里创建返回后的测试方法. package cn.gov.cma.cnn. ...
- mac终端(terminal)里的快捷键
Command + K 清屏 Command + T 新建标签 Command +W 关闭当前标签页 Command + S 保存终端输出 Command + D 垂直分隔当前标签页 Command ...
- 将double数据保留两位小数
private double formatDouble(double number) { DecimalFormat df = new DecimalFormat("#.00"); ...
- UI Framework-1: views
views Overview and background Windows provides very primitive tools for building user interfaces. Th ...
- mongodb 主从
mongodb 主从 因为条件限制我们把主从放在一台服务器上面 相关参数 在启动从的时候可以增加以下参数 --autoresync 当发现从服务器的数据不是最新时,开始从主服务器请求同步数据 --sl ...
- 并查集 (Union Find ) P - The Suspects
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...
- 一篇文章助你理解Python3中字符串编码问题
前几天给大家介绍了unicode编码和utf-8编码的理论知识,以及Python2中字符串编码问题,没来得及上车的小伙伴们可以戳这篇文章:浅谈unicode编码和utf-8编码的关系和一篇文章助你理解 ...