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. Datalist增删改查——联系人管理

    关于Datalist,其实和Repeater差不多,都是存放数据的控件,相比较下,Datalist和Repeater虽然都是用的模板,但是Datalist比之多了Edit模板,也就是编辑栏的模板,事件 ...

  2. Java 简单登录MVC

    构建一个简单的基于MVC模式的JavaWeb 零晨三点半了,刚刚几个兄弟一起出去吼歌,才回来,这应该是我大学第二次去K歌,第一次是大一吧,之后每次兄弟喊我,我都不想去,因为我还是很害怕去KTV,或许是 ...

  3. php网页显示正方形图片缩略图

    需求是这样的:原始图片的大小是不定的,类似800*600.1000*756,现有一个页面要以正方形(60*60)显示这些图片,注意:图片只能在内存处理,不能缩小后保存到本地磁盘. 解决办法: html ...

  4. 【转】MAC使用adb工具

    原文网址:http://www.jeffjade.com/2015/03/21/2015-03-21-android-adb/ 前阵子入手了一本MacPro后,终将阵地也转移到了这里.但是Mac默认不 ...

  5. (二)学习C#之内存管理

    一.当你运行你的程序通常都会访问哪些内存空间呢? 电脑自言自语道,“这个人要声明一个整数”或“这个人个方法”或“这个人要创建一个对象” 1.这些信息究竟是存在内存里的什么地方呢? 2.或者说用于描述这 ...

  6. 重温delphi之:如何将Bitmap位图与base64字符串相互转换

    先引用delphi自带的单元 uses EncdDecd; 然后就可以使用下面二个函数了: by 菩提树下的杨过 http://yjmyzz.cnblogs.com/ ///将Bitmap位图转化为b ...

  7. PowerShell:Linux程序员喜欢的cmd增强版

    Linux程序员有时偶尔使用Windows下的cmd工具,会被逼疯的,有些命令ls, cat, ps等已经条件反射一样使用. 但在cmd下,根本不知道该用什么命令,好在盖兹大叔照顾了此部分需求.从Vi ...

  8. Robotium自动化测试报告生成

    使用Robotium进行测试的时候,要想可以导出可视的测试结果,可以使用junitreport来实现junitreport下载地址:https://github.com/jsankey/android ...

  9. 【原】Redis事务管理

    Redis高级篇 事务 MULTI, EXEC, DISCARD and WATCH命令用于保证Redis中的事务处理 一个事务中的所有命令被序列化并串行执行. 事务的原子性. 用法 MULTI ...

  10. C/C++动态分配与释放内存的区别详细解析

    以下是对C与C++中动态分配与释放内存的区别进行了详细的分析介绍,需要的朋友可以过来参考下 1. malloc()函数1.1 malloc的全称是memory allocation,中文叫动态内存分配 ...