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)的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  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. [Redux] Understand Redux Higher Order Reducers

    Higher Order Reducers are simple reducer factories, that take a reducer as an argument and return a ...

  7. [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: ...

  8. [React] Write Compound Components

    Compound component gives more rendering control to the user. The functionality of the component stay ...

  9. [React Fundamentals] Composable Components

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

随机推荐

  1. bzoj1857

    三分的入门题,如果从AB上一点走到D是一个单峰函数从AB开始向CD传送带走的那个点也是一个单峰函数显然三分套三分 const eps=1e-5; var ax,ay,bx,by,cx,cy,dx,dy ...

  2. poj2478

    比较简单的树形dp; 定义s[i]为节点i的子树节点数和(包括自身):叶子节点s[j]=1; s[i]=signma(s[k])+1 (k是i的孩子) 则i满足的条件是 1.s[k]<=n di ...

  3. BZOJ2870: 最长道路tree

    题解: 子树分治的做法可以戳这里:http://blog.csdn.net/iamzky/article/details/41120733 可是码量... 这里介绍另一种好写又快的方法. 我们还是一颗 ...

  4. 【转】iOS中设置导航栏标题的字体颜色和大小

    原文网址:http://www.360doc.com/content/15/0417/11/20919452_463847404.shtml iOS中设置导航栏标题的字体颜色和大小,有需要的朋友可以参 ...

  5. 使用weka进行Cross-validation实验

    Generating cross-validation folds (Java approach) 文献: http://weka.wikispaces.com/Generating+cross-va ...

  6. 【转】 hive简介,安装 配置常见问题和例子

    原文来自:  http://blog.csdn.net/zhumin726/article/details/8027802 1 HIVE概述 Hive是基于Hadoop的一个数据仓库工具,可以将结构化 ...

  7. Altium Designer快捷键 【worldsing笔记】

    Shift + R 切换三种布线模式 (忽略, 避开或推挤) Shift + E 触发电气格点开/关 Shift + B 建立查询 Shift + PgUp 放大到最小的递增 Shift + PgDn ...

  8. DS18B20 for STM32 源代码 【worldsing笔记】

    DS18B20是DALLAS公司生产的一线式数字温度传感器,具有3引脚TO-92小体积封装形式:温度测量范围为-55℃-+125℃,可编程为9位-12位A/D转换精度,测温分辨率可达0.0625℃.主 ...

  9. Servlet+Tomcat 界面登录

    1.文件夹建立(必须按照这个规范,文件名和文件夹名必须一致) a.在%TOMCAT_HOME%\webapps下建立一个文件夹,取名MyWebsit b.在MyWebsit文件夹下新建WEB_INF文 ...

  10. MongoDB系列一(安装)

    一.MongoDB在Windows平台下的安装: 安装包官方下载地址:http://www.mongodb.org/downloads 第一步:下载安装包:如果是win系统,注意是64位还是32位版本 ...