[React] Higher Order Components (replaces Mixins)
Higher order components will allow you to apply behaviors to multiple React components.
So the idea is to create a high order component, then use this hight order component to create multi same behaivor component.
So high order function is insdie function return another function, the same, high order component is inside component return another component.
let Mixin = InnerComponent => class extends React.Component {
constructor(){
super();
this.state = {val: }
}
update(){
this.setState({val: this.state.val + })
}
componentWillMount(){
console.log('will mount')
}
render(){
return <InnerComponent
update={this.update.bind(this)}
{...this.state}
{...this.props} />
}
componentDidMount(){
console.log('mounted')
}
}
Inside the Mixin Component, we define the behavior "update" which will be shared to the mixined components.
Define two component:
const Button = (props) => <button
onClick={props.update}>
{props.txt} - {props.val}
</button> const Label = (props) => <label
onMouseMove={props.update}>
{props.txt} - {props.val}
</label>
Mixin those two component with Mixin:
let ButtonMixed = Mixin(Button)
let LabelMixed = Mixin(Label)
Use it:
class App extends React.Component {
render(){
return (
<div>
<ButtonMixed txt="Button" />
<LabelMixed txt="Label" />
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('app'));
-----
let Mixin = InnerComponent => class extends React.Component {
constructor(){
super();
this.state = {val: }
}
update(){
this.setState({val: this.state.val + })
}
componentWillMount(){
console.log('will mount')
}
render(){
return <InnerComponent
update={this.update.bind(this)}
{...this.state}
{...this.props} />
}
componentDidMount(){
console.log('mounted')
}
}
const Button = (props) => <button
onClick={props.update}>
{props.txt} - {props.val}
</button>
const Label = (props) => <label
onMouseMove={props.update}>
{props.txt} - {props.val}
</label>
let ButtonMixed = Mixin(Button)
let LabelMixed = Mixin(Label)
class App extends React.Component {
render(){
return (
<div>
<ButtonMixed txt="Button" />
<LabelMixed txt="Label" />
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('app'));
[React] Higher Order Components (replaces Mixins)的更多相关文章
- [React Intl] Use a react-intl Higher Order Component to format messages
In some cases, you might need to pass a string from your intl messages.js file as a prop to a compon ...
- [React] Cleanly Map Over A Stateless Functional Component with a Higher Order Component
In this lesson we'll create a Higher Order Component (HOC) that takes care of the key property that ...
- [React] Implement a Higher Order Component with Render Props
When making a reusable component, you'll find that people often like to have the API they're most fa ...
- 【转】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 - ...
- [Redux] Understand Redux Higher Order Reducers
Higher Order Reducers are simple reducer factories, that take a reducer as an argument and return a ...
- [CS61A] Lecture 5&6&7. Environments & Design & Functions Examples & Homework 2: Higher Order Functions
[CS61A] Lecture 5&6&7. Environments & Design & Functions Examples & Homework 2: ...
- [React] Write Compound Components
Compound component gives more rendering control to the user. The functionality of the component stay ...
- [React Fundamentals] Composable Components
To make more composable React components, you can define common APIs for similar component types. im ...
随机推荐
- SCOI2007修车
这样也行?这构图把我惊呆了: 把每个工人拆成N个点.记为A[i,j]表示第i个工人修倒数第j辆车. 每个车跟所有N*M个工人拆出的点连边.流量为1,费用为time[i,j]*k. 源和每辆车连边,N* ...
- [转] LCA与Tarjan
转载:http://m.blog.csdn.net/blog/u013076044/41875009# 在线算法与离线算法的定义 在计算机科学中,一个在线算法是指它可以以序列化的方式一个个的处理输入, ...
- 前端js框架收藏
1. AngularJS :AngularJS是为克服HTML在构建应用上的不足而设计的. 2. knockout 3. avalon :MVVM是前端究极的解决方案,因此之后我大多数时间都在折腾av ...
- 声明顺序 (Bootstrap 编码规范)
相关的属性声明应当归为一组,并按照下面的顺序排列: Positioning Box model Typographic Visual 由于定位(positioning)可以从正常的文档流中移除元素,并 ...
- C# 中的 ref 和 out 的意义和使用方法
原文C# 中的 ref 和 out 的意义和使用方法 向方法传递一个实参时,对应的形参会用实参的一个副本来初始化,不管形参是值类型(例如 int),可空类型(int?),还是引用类型,这一点都是成立的 ...
- e2e 自动化集成测试 架构 实例 WebStorm Node.js Mocha WebDriverIO Selenium Step by step (三) SqlServer数据库的访问
上一篇文章“e2e 自动化集成测试 架构 京东 商品搜索 实例 WebStorm Node.js Mocha WebDriverIO Selenium Step by step 二 图片验证码的识别” ...
- vs2013下git的使用
一.将VS2013项目托管到Git 现在的开源项目越来越多,我们会注意到这些开源项目大多共享在GitHub上面,包括微软开源的项目也在Github上,当然,如果你用过Git后,会发现Git确实不错,其 ...
- java对象和类学习
定义对象的类: 一个对象的状态(属性或特征)是指那些具有他们当前值的数据域 一个对象的行为是由方法定义的,调用对象的方法就是完成对象的一个动作 使用一个通用类来定义同一类型的对象.类是一个模板,一个对 ...
- 恒天云技术分享系列3 – KVM性能调优
恒天云技术分享:http://www.hengtianyun.com/download-show-id-11.html KVM是什么 KVM 是 kernel-based Virtual Machin ...
- HW7.3
public class Solution { public static void main(String[] args) { char[][] answers = { {'A', 'B', 'A' ...