react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / core / ReactContext.js
81152 views/**1* Copyright 2013-2014, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under the BSD-style license found in the5* LICENSE file in the root directory of this source tree. An additional grant6* of patent rights can be found in the PATENTS file in the same directory.7*8* @providesModule ReactContext9*/1011"use strict";1213var assign = require('Object.assign');1415/**16* Keeps track of the current context.17*18* The context is automatically passed down the component ownership hierarchy19* and is accessible via `this.context` on ReactCompositeComponents.20*/21var ReactContext = {2223/**24* @internal25* @type {object}26*/27current: {},2829/**30* Temporarily extends the current context while executing scopedCallback.31*32* A typical use case might look like33*34* render: function() {35* var children = ReactContext.withContext({foo: 'foo'}, () => (36*37* ));38* return <div>{children}</div>;39* }40*41* @param {object} newContext New context to merge into the existing context42* @param {function} scopedCallback Callback to run with the new context43* @return {ReactComponent|array<ReactComponent>}44*/45withContext: function(newContext, scopedCallback) {46var result;47var previousContext = ReactContext.current;48ReactContext.current = assign({}, previousContext, newContext);49try {50result = scopedCallback();51} finally {52ReactContext.current = previousContext;53}54return result;55}5657};5859module.exports = ReactContext;606162