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 ...
随机推荐
- Android-Activity使用(2) -传值
一.简单传值 1.修改MainActivity protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedIns ...
- VS2010的Razor智能感知和语法高亮突然消失
猜想可能是安装了VS2008的原因,尝试重新安装下面的组件,看看是否解决问题: 用于 Visual Studio 2010 SP1 和 Visual Web Developer 2010 SP1 的 ...
- linux的命令
Linux命令的分类 选项及参数的含义 以"-"引导短格式选项的(单个字符),例如"-l" 以"--"引导长格式选项(多个字符),例如&qu ...
- jsp 环境配置记录
1. jdk,下载地址1 环境变量配置: 1)新建 JAVA_HOME 变量 . 变量值填写jdk的安装目录(本人是 C:\Java\jdk1.7.0) 2) 系统变量→寻找 Path 变量→编辑 ...
- cellspacing,cellpadding什么区别
cellspacing设置为“0”,显示的结果就是第一个表格的每个单元格之间的距离为0.若将表格边框设为“0”,则单元格 的距离就是0了cellpadding属性用来指定单元格内容与单元格边界之间的空 ...
- sqlserver中对时间类型的字段转换
获取当前日期利用 convert 来转换成我们需要的datetime格式. select CONVERT(varchar(12) , getdate(), 112 ) 20040912-------- ...
- StringBuilder和Append的一个程序及一个基础概念
废话少说直接来说:比如在串口数据操作中,我们只想显示串口接收的字符串,好吧你用string[]吧,有多少个字符串(顺便说下二进制在C#中是以字符串形式出现的)就要分配多少个储存空间,自己试下,要你你干 ...
- 【转】 #1451 - Cannot delete or update a parent row: a foreign key constraint fails 问题的解决办法
转载地址:http://blog.csdn.net/donglynn/article/details/17056099 错误 SQL 查询: DELETE FROM `zmax_lang` WHERE ...
- centos7忘记root密码修改方式
1.在进入系统选择时按下e键
- WF(二)
步骤一: 运用WF(一)中创建好的solution 重命名Workflow1.xaml,变为SayHello.xaml 并在属性窗口设置名称为HelloWorkflow.SayHello,如下图: ( ...