[React] Higher Order Components (replaces Mixins)

时间:2022-01-24 09:15:52

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. &lbrack;React Intl&rsqb; 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. &lbrack;React&rsqb; 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. &lbrack;React&rsqb; 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 译者前言 这是一篇来自 * 的问答,提问的人认为 Rea ...

  5. Facebook React 和 Web Components(Polymer)对比优势和劣势

    目录结构 译者前言 Native vs. Compiled 原生语言对决预编译语言 Internal vs. External DSLs 内部与外部 DSLs 的对决 Types of DSLs - ...

  6. &lbrack;Redux&rsqb; Understand Redux Higher Order Reducers

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

  7. &lbrack;React&rsqb; Write Compound Components

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

  8. &lbrack;React Fundamentals&rsqb; Composable Components

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

  9. &lbrack;React&rsqb; React Router&colon; Named Components

    In this lesson we'll learn how to render multiple component children from a single route. Define a n ...

随机推荐

  1. C&num;的惰性枚举

    Ruby 2.0有一个新的特性是惰性枚举器,Soi Mort 的博客举了一个例子:可以将下面的代码 File.open(path) {|fp| fp.each_line. \ select {|lin ...

  2. intellij 设置-试验过的

    1.已修改的文件星号“*”标记 2.在PROJECT窗口中快速定位,编辑窗口中的文件 在编辑的所选文件按ALT+F1, 然后选择PROJECT VIEW 3.改变编辑文本字体大小 FILE -> ...

  3. springMVC(spring)&plus;WebSocket案例(获取请求参数)

    开发环境(最低版本):spring 4.0+java7+tomcat7.0.47+sockjs 前端页面要引入: <script src="http://cdn.jsdelivr.ne ...

  4. JDK的下载,安装,环境变量配置

    JDK 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 环境变量配置:在"系统变量" ...

  5. dubbo接口demo开发

    接口需求 客户端输入uncleyong(当然,也可以输入其它字符串),服务端返回hello uncleyong 开发环境 jdk + idea + maven + zookeeper jdk安装 id ...

  6. Scala 入门详解

    Scala 入门详解 基本语法 Scala 与 Java 的最大区别是:Scala 语句末尾的分号 ; 是可选的 Scala 程序是对象的集合,通过调用彼此的方法来实现消息传递.类,对象,方法,实例变 ...

  7. android官方资料

    android develop – Guides http://developer.android.com/guide/ android develop – API Reference http:// ...

  8. jQuery获取select中全部option值

    <select id="language"> <option value="">请选择</option> <optio ...

  9. hdu1159Common Subsequence(动态规划)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  10. SpringMVC10 InitBinder 注册自定义编辑器

    1.配置web.xml文件 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3// ...