资讯专栏INFORMATION COLUMN

Reactjs Mixins

xfee / 554人阅读

摘要:抛砖引玉实现一个日志功能。一般都会抽出公共的部分。看起来挺完美的,实际上还是有个问题代码的耦合性太强像这种日志功能应该同业务分离,不应该直接出现在业务代码中。如果做过开发,应该很容易想到一个概念面向切面编程。

抛砖引玉

实现一个日志功能。

组件在挂载前打印 Component will mount

组件挂载后打印 Component did mount

不能忍受的写法
var AComponent = React.createClass({
    componentWillMount: function () {
        console.log("Component will mount");
    },
    componentDidMount: function () {
        console.log("Component did mount");
    },
    render: function () {
        return (
            
AComponent
) } }); var BComponent = React.createClass({ componentWillMount: function () { console.log("Component will mount"); }, componentDidMount: function () { console.log("Component did mount"); }, render: function () { return (
BComponent
) } });

看到上面的代码,直接吐血而亡啊,写的是什么几把玩意儿。还好只写了两个组件,要是多个组件,相同的代码就会重复多遍。相信大家看到上面的代码也会发现一个致命的问题:可维护性太差差差!

改进

相信大家都不会写出上面的代码,如果真有人会那样写,请允许我呵呵你。一般都会抽出公共的部分。

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 (
            
AComponent
) } }); var BComponent = React.createClass({ componentWillMount: function () { Log.componentWillMount(); }, componentDidMount: function () { Log.componentDidMount(); }, render: function () { return (
BComponent
) } });

看起来挺完美的,实际上还是有个问题:代码的耦合性太强!像这种日志功能应该同业务分离,不应该直接出现在业务代码中。如果做过Java开发,应该很容易想到一个概念:AOP—面向切面编程。

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 (
            
AComponent
) } }); var BComponent = React.createClass({ mixins: [LogMixin], render: function () { return (
BComponent
) } });

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 (
            
Component
) } });

控制台打印:

$ 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 (
            
Component
) } });

控制台打印:

$ 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 (
            
Component
) } });

控制台打印:

$ 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 (
            
Component
) } });
情景2
var Mixin = {
    log: function () {
        console.log("Mixin Log");
    }
};

var Component = React.createClass({
    mixins: [Mixin],
    log:function(){
        console.log("Component Log");
    },
    render: function () {
        return (
            
Component
) } });

以上两种情景,都会报错,控制信息如下,所以使用的时候要小心了

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/85654.html

相关文章

  • 翻译 | 从 ReactJS 到 React-Native—两者的主要差异是什么?

    摘要:跟非常相似,但是在开始尝试你第一个之前,也需要了解两者之间的一些差异。推荐的方式是使用提供的。能用到组件中的或者上以启用这个组件的触摸事件。 华翔,Web前端开发工程师著作权归作者所有,转载请联系作者获得授权。 showImg(https://segmentfault.com/img/bVUliz?w=640&h=235); React-Native已经诞生有两年左右了,自从适配了An...

    darkerXi 评论0 收藏0
  • Reactjs快速上手

    摘要:当或的值发生变化时触发,并通过返回值确定是否触发更新事件但初始化组件时不会触发。该方式仅在未使用的时候适用。 React简述 React是由fackbook开放的一个构建用户接口的javascript类库,其主要目的是为了开发随时间数据不断变化的大型应用程序,许多开发者将React视作为MVC模式中的V.2011年React开始被fackbook工程师着手开发,并作为内部使用,2013...

    zzzmh 评论0 收藏0
  • React 、 ES6 - 介绍(第一部分)

    摘要:下一步我们将结果输出到文件。这是我们用编写的第一个非常简单的组建。使用将创建的组建导出以便在其它地方能够正常导入使用。 这是React和ECMAScript6结合使用系列文章的第一篇。 本文出自从零到壹全栈部落 下面是所有系列文章章节的链接: React 、 ES6 - 介绍(第一部分) React类、ES7属性初始化(第二部分) React类,方法绑定(第三部分) ES6中Reac...

    pingink 评论0 收藏0
  • REST API设计理念与 Python Mixin模式

    摘要:飞机就是一种交通工具,可飞行的能力是是飞机的属性,通过继承接口来获取语言可没有接口功能,但是它可以多重继承。说是,因为从语法上看,的确是通过多重继承实现的。所以从含义上理解,只是一个,不是一个。比如飞机照样可以载客,就是不能飞了 REST API设计理念 showImg(https://segmentfault.com/img/remote/1460000019923606);sho...

    waruqi 评论0 收藏0
  • REST API设计理念与 Python Mixin模式

    摘要:飞机就是一种交通工具,可飞行的能力是是飞机的属性,通过继承接口来获取语言可没有接口功能,但是它可以多重继承。说是,因为从语法上看,的确是通过多重继承实现的。所以从含义上理解,只是一个,不是一个。比如飞机照样可以载客,就是不能飞了 REST API设计理念 showImg(https://segmentfault.com/img/remote/1460000019923606);sho...

    iflove 评论0 收藏0

发表评论

0条评论

xfee

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<