React context

介绍

Contexts 是React的一个重要属性,可是到目前为止,这个属性在正式的文档里面尚未对它进行正式介绍,在 reactv0.1.4将会正式发布这个属性。下面先来介绍一下它的使用方式。javascript

React.withContext

React.withContext 会执行一个指定的上下文信息的回调函数,任何在这个回调函数里面渲染的组件都有这个context的访问权限。java

var A = React.createClass({

    contextTypes: {
        name: React.PropTypes.string.isRequired,
    },

    render: function() {
        return <div>My name is: {this.context.name}</div>;
    }
});

React.withContext({'name': 'Jonas'}, function () {
    // Outputs: "My name is: Jonas"
    React.render(<A />, document.body);
});

任何想访问context里面的属性的组件都必须显式的指定一个contextTypes 的属性。若是没有指定改属性,那么组件经过 this.context 访问属性将会出错。react

若是你为一个组件指定了context,那么这个组件的子组件只要定义了contextTypes 属性,就能够访问到父组件指定的context了。函数

var A = React.createClass({

    render: function() {
         return <B />;
    }
});

var B = React.createClass({

    contextTypes: {
        name: React.PropTypes.string
    },

    render: function() {
        return <div>My name is: {this.context.name}</div>;
    }
});

React.withContext({'name': 'Jonas'}, function () {
   React.render(<A />, document.body);
});

为了减小文件的引用,你能够为contextTypes 放到一个minx 中,这样 用到的组件引用这个 minx 就好了。ui

var ContextMixin = {
    contextTypes: {
        name: React.PropTypes.string.isRequired
    },

    getName: function() {
        return this.context.name;
    }
};

var A = React.createClass({

    mixins: [ContextMixin],

    render: function() {
         return <div>My name is {this.getName()}</div>;
    }
});

React.withContext({'name': 'Jonas'}, function () {
    // Outputs: "My name is: Jonas"
    React.render(<A />, document.body);
});

getChildContext

和访问context 的属性是须要经过 contextTypes 指定可访问的 元素同样。getChildContext 指定的传递给子组件的属性须要先经过 childContextTypes 来指定,否则会产生错误。this

// This code *does NOT work* becasue of a missing property from childContextTypes
var A = React.createClass({

    childContextTypes: {
         // fruit is not specified, and so it will not be sent to the children of A
         name: React.PropTypes.string.isRequired
    },

    getChildContext: function() {
         return {
             name: "Jonas",
             fruit: "Banana"
         };
    },

    render: function() {
         return <B />;
    }
});

var B = React.createClass({

    contextTypes: {
        fruit: React.PropTypes.string.isRequired
    },

    render: function() {
        return <div>My favorite fruit is: {this.context.fruit}</div>;
    }
});


// Errors: Invariant Violation: A.getChildContext(): key "fruit" is not defined in childContextTypes.
React.render(<A />, document.body);

假设你的应用程序有多层的context。经过withContextgetChildContext 指定的context元素均可以被子组件引用。可是子组件是须要经过 contextTypes 来指定所须要的context 元素的。code

var A = React.createClass({

    childContextTypes: {
         fruit: React.PropTypes.string.isRequired
    },

    getChildContext: function() {
         return { fruit: "Banana" };
    },

    render: function() {
         return <B />;
    }
});

var B = React.createClass({

    contextTypes: {
        name: React.PropTypes.string.isRequired,
        fruit: React.PropTypes.string.isRequired
    },

    render: function() {
        return <div>My name is: {this.context.name} and my favorite fruit is: {this.context.fruit}</div>;
    }
});

React.withContext({'name': 'Jonas'}, function () {
    // Outputs: "My name is: Jonas and my favorite fruit is: Banana"
    React.render(<A />, document.body);
});

context 是就近引用的,若是你经过withContext 指定了context元素,而后又经过 getChildContext 指定了该元素,该元素的值将会被覆盖。ip

var A = React.createClass({

    childContextTypes: {
         name: React.PropTypes.string.isRequired
    },

    getChildContext: function() {
         return { name: "Sally" };
    },

    render: function() {
         return <B />;
    }
});

var B = React.createClass({

    contextTypes: {
        name: React.PropTypes.string.isRequired
    },

    render: function() {
        return <div>My name is: {this.context.name}</div>;
    }
});

React.withContext({'name': 'Jonas'}, function () {
    // Outputs: "My name is: Sally"
    React.render(<A />, document.body);
});

总结

经过context传递属性的方式能够大量减小 经过显式的经过 props 逐层传递属性的方式。这样能够减小组件之间的直接依赖关系。ci

相关文章
相关标签/搜索