react mixins编写


var LogMixin = {
componentWillMount: function () {
console.log('Component will mount');
},
componentDidMount: function () {
console.log('Component did mount');
}
};
var AComponent = React.createClass({
mixins: [LogMixin],
render: function () {
return (
<div>AComponent</div>
)
}
});
var BComponent = React.createClass({
mixins: [LogMixin],
render: function () {
return (
<div>BComponent</div>
)
}
});
mixins的详细学习:https://segmentfault.com/a/1190000002704788
Mixins
利用React的Mixins,编写的代码就像这样的:
var LogMixin = {
componentWillMount: function () {
console.log('Component will mount');
},
componentDidMount: function () {
console.log('Component did mount');
}
};
var AComponent = React.createClass({
mixins: [LogMixin],
render: function () {
return (
<div>AComponent</div>
)
}
});
var BComponent = React.createClass({
mixins: [LogMixin],
render: function () {
return (
<div>BComponent</div>
)
}
});
Mixins有点类似AOP。所谓的mixins就是将组件里的方法抽出来。实际上Mixins里的this是指向组件的,使用了Mixins以后,组件也可以调用Mixins里的方法。
组件调用Mixins方法
var Mixin = {
log:function(){
console.log('Mixin log');
}
};
var Component = React.createClass({
mixins: [Mixin],
componentWillMount: function () {
this.log();
},
render: function () {
return (
<div>Component</div>
)
}
});
控制台打印:
$ Mixin log
生命周期方法
Mixins里也可以编写组件生命周期的方法,需要注意的是:Mixins里的方法并不会覆盖组件的生命周期方法,会在先于组件生命周期方法执行。
var Mixin = {
componentWillMount: function () {
console.log('Mixin Will Mount');
}
};
var Component = React.createClass({
mixins: [Mixin],
componentWillMount: function () {
console.log('Component Will Mount');
},
render: function () {
return (
<div>Component</div>
)
}
});
控制台打印:
$ Mixin Will Mount
$ Component Will Mount
使用多个Mixin
组件也可以使用多个Mixin
var AMixin = {
componentWillMount: function () {
console.log('AMixin Will Mount');
}
};
var BMixin = {
componentWillMount: function () {
console.log('BMixin Will Mount');
}
};
var Component = React.createClass({
mixins: [AMixin,BMixin],
componentWillMount: function () {
console.log('Component Will Mount');
},
render: function () {
return (
<div>Component</div>
)
}
});
控制台打印:
$ AMixin Will Mount
$ BMixin Will Mount
$ Component Will Mount
数组引入的顺序,决定了Mxins里生命周期方法的执行顺序。
不允许重复
除了生命周期方法可以重复以外,其他的方法都不可以重复,否则会报错
情景1
var AMixin = {
log: function () {
console.log('AMixin Log');
}
};
var BMixin = {
log: function () {
console.log('BMixin Log');
}
};
var Component = React.createClass({
mixins: [AMixin,BMixin],
render: function () {
return (
<div>Component</div>
)
}
});
情景2
var Mixin = {
log: function () {
console.log('Mixin Log');
}
};
var Component = React.createClass({
mixins: [Mixin],
log:function(){
console.log('Component Log');
},
render: function () {
return (
<div>Component</div>
)
}
});
以上两种情景,都会报错,控制信息如下,所以使用的时候要小心了
react mixins编写的更多相关文章
- React与ES6(四)ES6如何处理React mixins
React与ES6系列: React与ES6(一)开篇介绍 React和ES6(二)ES6的类和ES7的property initializer React与ES6(三)ES6类和方法绑定 React ...
- React Mixins
[React Mixins] ES6 launched without any mixin support. Therefore, there is no support for mixins whe ...
- 用React Native编写跨平台APP
用React Native编写跨平台APP React Native 是一个编写iOS与Android平台实时.原生组件渲染的应用程序的框架.它基于React,Facebook的JavaScript的 ...
- 4. react 基础 - 编写 todoList 功能
编写 TodoList 功能 react 入口 js #src/index.js import React from 'react'; import ReactDOM from 'react-dom' ...
- 利用React/anu编写一个弹出层
本文将一步步介绍如何使用React或anu创建 一个弹出层. React时代,代码都是要经过编译的,我们很多时间都耗在babel与webpack上.因此本文也介绍如何玩webpack与babel. 我 ...
- react better-scroll 编写类似手机chrome的header显示隐藏效果
关键代码 const H = 50; // header的高度 const H2 = H / 2; let cy = 0; class Home extends Component { @observ ...
- react+mobx 编写 withStoreHistory 装饰器
主要作用是向store里面注入一个history对象,方便story里面的函数调用 function withStoreHistory(storeName) { if (!storeName) ret ...
- react+mobx 编写 Async装饰器
使用 // indexStore.js import axios from "axios"; import { from } from "rxjs"; impo ...
- Node.js建站笔记-使用react和react-router取代Backbone
斟酌之后,决定在<嗨猫>项目中引入react,整体项目偏重spa模式,舍弃部分server端的模板渲染,将一部分渲染工作交给前端react实现. react拥有丰富的组件,虽然不如Back ...
随机推荐
- DOM9大节点
ELEMENT_NODE 1 元素节点 常用 ATTRIBUTE_NODE 2 属性节点 常用 TEXT_NODE 3 文本节点 常用 CDATA_SECTION_NODE 4 CDATA区段 E ...
- ios之json,xml解析
JSON解析步骤: 1.获取json文件路径 NSString*path = [[NSBundle mainBundle] pathForResource:@"Teacher"of ...
- Sql Server 性能优化之包含列
导读:数据数优化查询一直是个比较热门的话题,小生在这方面也只能算是个入门生.今 天我们就讲下数据库包含列这个一项的作用及带来的优化效果 引用下MSDN里面的一段解释: 当查询中的所有列都作为键列或非键 ...
- RW-50004 While Running adrunfmw during EBS 12.2 Installation
安装过程中报错: 日志文件信息: Executing command: /app/R1220/startCD/Disk1/rapidwiz/jre/Linux_x64//bin/java -cp /a ...
- # java对xml文件的基本操作
下面是简单的总结三种常用的java对xml文件的操作 1. dom方式对xml进行操作,这种操作原理是将整个xml文档读入内存总,在内存中进行操作,当xml文档非常庞大的时候就会出现内存溢出的异常,这 ...
- 解决Ruby在IE11中报Unable to get browser (Selenium::WebDriver::Error::NoSuchWindowError)的错误
转载地址:http://www.tuicool.com/articles/BRnqeu2 I was updating the browser WebDrivers for Seleno ...
- 扩大Tomcat内存
在Eclipse里选:Window->Preference->Installed JREs->Edit(选中jre),在Default VM Arguments里输入-Xms256m ...
- tp-link便携式路由器固件升级方式
系统: win-7 32bit 关闭杀毒软件,关闭防火墙(保险起见) 各位从tp-link的官网下载自己对应的固件 http://www.tp-link.com.cn/product/list_por ...
- zigbee学习之路(十):串口(接收)
一.前言 上次我们学习了串口的发送,今天我们要学习串口的接收,要实现的功能是接收电脑发来的数据,控制LED 灯闪烁,而且将收到的数据发回给电脑显示出来.而且要采用不占用cpu资源的中断. 二原理与分析 ...
- zigbee学习之路(二)点亮LED
一.前言 今天,我来教大家如何点亮led,这也是学习开发板最基础的步骤了. 二.原理分析 cc2530芯片跟虽然是51的内核,但是它跟51单片机还是有区别的,51单片机不需要对IO口进行配置,而cc2 ...