实现一个日志功能。编程
var AComponent = React.createClass({ componentWillMount: function () { console.log('Component will mount'); }, componentDidMount: function () { console.log('Component did mount'); }, render: function () { return ( <div>AComponent</div> ) } }); var BComponent = React.createClass({ componentWillMount: function () { console.log('Component will mount'); }, componentDidMount: function () { console.log('Component did mount'); }, render: function () { return ( <div>BComponent</div> ) } });
看到上面的代码,直接吐血而亡啊,写的是什么几把玩意儿。还好只写了两个组件,要是多个组件,相同的代码就会重复多遍。相信你们看到上面的代码也会发现一个致命的问题:可维护性太差差差!数组
相信你们都不会写出上面的代码,若是真有人会那样写,请容许我呵呵你。通常都会抽出公共的部分。this
var Log = { componentWillMount: function () { console.log('Component will mount'); }, componentDidMount: function () { console.log('Component did mount'); } }; var AComponent = React.createClass({ componentWillMount: function () { Log.componentWillMount(); }, componentDidMount: function () { Log.componentDidMount(); }, render: function () { return ( <div>AComponent</div> ) } }); var BComponent = React.createClass({ componentWillMount: function () { Log.componentWillMount(); }, componentDidMount: function () { Log.componentDidMount(); }, render: function () { return ( <div>BComponent</div> ) } });
看起来挺完美的,实际上仍是有个问题:代码的耦合性太强!像这种日志功能应该同业务分离,不该该直接出如今业务代码中。若是作过Java开发,应该很容易想到一个概念:AOP—面向切面编程。spa
利用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里的方法。code
var Mixin = { log:function(){ console.log('Mixin log'); } }; var Component = React.createClass({ mixins: [Mixin], componentWillMount: function () { this.log(); }, render: function () { return ( <div>Component</div> ) } });
控制台打印:component
$ Mixin log
Mixins里也能够编写组件生命周期的方法,须要注意的是:Mixins里的方法并不会覆盖组件的生命周期方法,会在先于组件生命周期方法执行。blog
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图片
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里生命周期方法的执行顺序。
除了生命周期方法能够重复之外,其余的方法都不能够重复,不然会报错
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> ) } });
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> ) } });
以上两种情景,都会报错,控制信息以下,因此使用的时候要当心了