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. 函数page_get_space_id

    #define FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID  34 /****************************************************** ...

  2. chromium的部署工具depot_tools和gclient

    depot_tools是个工具包,里面包含gclient.gcl.gn和ninja等工具.其中gclient是代码获取工具,它其实是利用了svn和git.主要涉及的depot_tools文件夹下的文件 ...

  3. C# List 用法与示例

    Problem. You have questions about the List collection in the .NET Framework, which is located in the ...

  4. LeetCode Binary Tree Level Order Traversal (按层收集元素)

    题意:按层,将元素收集在一个二维数组中. 思路:广搜应该是普遍的方法了.还能避免栈溢出,多好用.搭配deque,因为要经常删除. /** * Definition for a binary tree ...

  5. 每天学点linux命令--tail,cut,sort,uniq

    tail 命令从指定点开始将文件写到标准输出.使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不但刷新, ...

  6. VTK三维重建(1)-使用VTK读取DICOM,并动态输出

    [效果显示] 将脚部骨骼扫描的CT照片进行的连续读取, 运行结果存为了两个动态gif, 不知道能不能正常显示 [程序实现] int main(int argc, char* argv[]) { // ...

  7. WebOb的简单介绍

    在之前的文章中我写了有关于如何使用PasteDeploy生成WSGI的Application.在Openstack的源码中,除了 PasteDeploy外,还有一个和WSGI密切相关的工具包WebOb ...

  8. leetcode–Binary Tree Maximum Path Sum

    1.题目说明 Given a binary tree, find the maximum path sum.   The path may start and end at any node in t ...

  9. Redhat常见问题

    1.现象:hadoop用户启动startx时失败,报如下提示 Fatal server error: PAM authentication failed, cannot start X server. ...

  10. Android的Spinner

    使用Spinner遇到不少坑啊 3.自定义spinner样式 <style name="AppTheme" parent="Theme.AppCompat.Ligh ...