[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 ...
随机推荐
- [原]Unity3D深入浅出 - 认识开发环境中的GameObject菜单栏
Create Empty:创建空对象 Create Other:创建其他对象 Particle System:创建粒子系统 Camera:创建相机 GUI Text:GUI文本 GUI Texture ...
- POJ 1149 PIGS ★(经典网络流构图)
[题意] 有M个猪圈,每个猪圈里初始时有若干头猪.一开始所有猪圈都是关闭的.依 次来了N个顾客,每个顾客分别会打开指定的几个猪圈,从中买若干头猪.每 个顾客分别都有他能够买的数量的上限.每个顾客走后, ...
- Linux kernel Makefile for ctags
/********************************************************************** * Linux kernel Makefile for ...
- I.MX6 gpio-keys driver hacking
/**************************************************************************** * I.MX6 gpio-keys driv ...
- Android学习系列(15)--App列表之游标ListView(索引ListView)
游标ListView,提供索引标签,使用户能够快速定位列表项. 也可以叫索引ListView,有的人称也为Tweaked ListView,可能更形象些吧. 一看图啥都懂了: 1. ...
- 静态Web开发 DOM
四章 Dom 1节dom中的顶级对象 dom(文档对象模型)js最终是要操作html页面,让html变成DHtml,而操作Html页面就要用到DOMDOM可以吧Html页面模拟成一个对象,如果js只是 ...
- andorid 进度条
SeekBar类似于ProgressBar,但是ProgressBar的主要功能是让用户知道目前的状态,而SeekBar的功能在于让用户调整进度,举个例子,在音乐播放器中,可以通过调整SeekBar来 ...
- 方格取数(1)(HDU 1565状压dp)
题意: 给你一个n*n的格子的棋盘,每个格子里面有一个非负数. 从中取出若干个数,使得任意的两个数所在的格子没有公共边,就是说所取的数所在的2个格子不能相邻,并且取出的数的和最大. 分析:直接枚举 ...
- [OFBiz]简介 一
1.What is Apache OFBiz?http://ofbiz.apache.org/ 2.概述http://baike.baidu.com/view/638900.html?fromTagl ...
- LeetCode题解——Median of Two Sorted Arrays
题目: 找两个排序数组A[m]和B[n]的中位数,时间复杂度为O(log(m+n)). 解法: 更泛化的,可以找第k个数,然后返回k=(m+n)/2时的值. 代码: class Solution { ...