React Mixins

  ES6 launched without any mixin support. Therefore, there is no support for mixins when you use React with ES6 classes.  

  We also found numerous issues in codebases using mixins, and don't recommend using them in the new code.

var SetIntervalMixin = {
componentWillMount: function() {
this.intervals = [];
},
setInterval: function() {
this.intervals.push(setInterval.apply(null, arguments));
},
componentWillUnmount: function() {
this.intervals.forEach(clearInterval);
}
}; var createReactClass = require('create-react-class'); var TickTock = createReactClass({
mixins: [SetIntervalMixin], // Use the mixin
getInitialState: function() {
return {seconds: 0};
},
componentDidMount: function() {
this.setInterval(this.tick, 1000); // Call a method on the mixin
},
tick: function() {
this.setState({seconds: this.state.seconds + 1});
},
render: function() {
return (
<p>
React has been running for {this.state.seconds} seconds.
</p>
);
}
}); ReactDOM.render(
<TickTock />,
document.getElementById('example')
);

  If a component is using multiple mixins and several mixins define the same lifecycle method, all of the lifecycle methods are guaranteed to be called.   Methods defined on mixins run in the order mixins were listed, followed by a method call on the component.

  Mixins已经被证明坑太多,当你需要重用组件时,优先考虑HOC。

参考:https://facebook.github.io/react/docs/react-without-es6.html#mixins

React Mixins的更多相关文章

  1. React与ES6(四)ES6如何处理React mixins

    React与ES6系列: React与ES6(一)开篇介绍 React和ES6(二)ES6的类和ES7的property initializer React与ES6(三)ES6类和方法绑定 React ...

  2. react mixins编写

    var LogMixin = { componentWillMount: function () { console.log('Component will mount'); }, component ...

  3. React与ES6(三)ES6类和方法绑定

    React与ES6系列: React与ES6(一)开篇介绍 React和ES6(二)ES6的类和ES7的property initializer React与ES6(三)ES6类和方法绑定 React ...

  4. React和ES6(二)ES6的类和ES7的property initializer

    React与ES6系列: React与ES6(一)开篇介绍 React和ES6(二)ES6的类和ES7的property initializer React与ES6(三)ES6类和方法绑定 React ...

  5. React与ES6(一)开篇介绍

    React与ES6系列: React与ES6(一)开篇介绍 React和ES6(二)ES6的类和ES7的property initializer React与ES6(三)ES6类和方法绑定 React ...

  6. es6语法重构react代码

    1.使用React.Component创建组件,需要通过在constructor中调用super()将props传递给React.Component.另外react 0.13之后props必须是不可变 ...

  7. React.createClass和extends Component的区别

    React.createClass和extends Component的区别主要在于: 语法区别 propType 和 getDefaultProps 状态的区别 this区别 Mixins 语法区别 ...

  8. 转载 React.createClass 对决 extends React.Component

    先给出结论,这其实是殊途同归的两种方式.过去我们一般都会使用 React.createClass 方法来创建组件,但基于 ES6 的小小语法糖,我们还可以通过 extends React.Compon ...

  9. React进阶篇学习

    继续上一次基础篇, 分享一些关于React的进阶技术 React 进阶部分 ** context ** ** setState vs forceUpdate ** ** Mixins ** ** HO ...

随机推荐

  1. 【Jmeter自学】JMeter的安装(一)

    ==================================================================================================== ...

  2. Ubantu下安装jdk 教程

    ubuntu 安装jdk 的两种方式: 这篇帖子,有些地方,写的有点问题.我只能按照记忆中的印象,修改一下. 1:通过ppa(源) 方式安装. 2:通过官网下载安装包安装. 这里推荐第1种,因为可以通 ...

  3. CentOS6.9 网络设置

    一.临时设置IP地址 ifconfig eth0 192.168.42.119 broadcast 192.168.42.129 netmask 255.255.255.0 二.上述方法只能临时生效, ...

  4. 科赫曲线和科赫雪花的绘制Python

    #KochDrawV1.pyimport turtledef koch(size,n): if n == 0: turtle.fd(size) else: for angle in [0,60,-12 ...

  5. 白鹭引擎 - 显示对象与 HelloWord ( 绘制了一个红蓝相间的 2 x 2 格子 )

    1: 白鹭引擎默认实在一个 640 * 1136 的画布上作画 2: 入口文件 Main.ts,  类 Main 是程序的入口 // 1, 在一个宽高为 640 * 1136 的画布上作画 // 2, ...

  6. JsonConvert

    ///"{'jsonParam' : " + jsonText + "}" /* Dictionary<string, object> tmp = ...

  7. leetcode329

    public class Solution { bool[,] tags;//用于标记是否已经访问过,false未访问,true已访问 int[,] records;//用于标记以当前为起点的最长升序 ...

  8. <记录> PHP Redis操作类

    namespace common\controller; class Redis { public $redisObj = null; //redis实例化时静态变量 static protected ...

  9. Appium环境安装

    Appium 是开源的.跨平台的.多语言支持的 移动应用 自动化工具 原生app,如计算器 混合(Hybrid)app 内嵌web + 原生 移动 web app 手机浏览器打开的网址 安装appiu ...

  10. python day10 数据库(mysql基础)

    一.数据库的概念 数据:事物的特征 数据库的本质是:通过套接字进行通信,来读存数据的一种软件,由于每次开发人员写程序都得写数据的套接字,所以诞生了数据库这个软件,减少重复劳动.(sql语句通用) 数据 ...