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. Linux中使用python测试主机存活 Linux系统CentOS Linux release 7.3.1611 (Core) py版本Python 2.7.5

    下面是最初的情况 #/usr/bin/env python # -*- coding: utf-8 -*- import os import time import subprocess import ...

  2. maven 插件2

    Maven is - at its heart - a plugin execution framework; all work is done by plugins. Maven 本质上就是一个插件 ...

  3. 又见 tomcat启动startup.bat一闪而过

    startup.bat启动的时候,一闪而过,停止, 没有提示信息,错误信息,没有任何log... 后面在 startup.bat. catalina.bat 最后 加入 pause. 也看不到结果.. ...

  4. jsp grid can not be used in this ('quirks') mode

    设置: <!--设置IE文档模式  --> <meta http-equiv="X-UA-Compatible" content="IE=9" ...

  5. Redis zset数据类型

    zadd():添加元素 zcard :返回元素个数

  6. 深度学习原理与框架-RNN网络架构-RNN网络 1.RNN的前向传播 2.RNN的反向传播

    对于神经网络而言,每一个样本的输入与输入直接都是独立的,即预测的结果之间并没有联系 而对于RNN而言:不仅仅是有当前的输入,而且上一层的隐藏层也将进行输入,用于进行结果的预测.因此每一个输入都与之前的 ...

  7. 08-认识margin

    1.margin margin:外边距的意思.表示边框到最近盒子的距离. /*表示四个方向的外边距离为20px*/ margin: 20px; /*表示盒子向下移动了30px*/ margin-top ...

  8. html:块级元素和行内元素的特点

    display:block: 块元素会独自占据一整行,或者多行,可以任意设置其大小尺寸,是用于搭建网页布局的必须部分,使网页结构更加紧凑合理. 块级元素width.height.padding.mar ...

  9. 用python实现一个简单的服务器

    打开命令行工具,输入: python3 -m http.server 8000(端口可以自己定) 通过访问:http://ip:8000/,就能给别人快速分享文件了.

  10. 如何判定耿耿数已经被bind过?

    这个只有原生bind才有这福利 var fn1 = function(){} var a = {} var b = fn1.bind(a) console.log(b.name) // "b ...