mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
f5038829d8
This ensures that we have a prefix that can be easily identified in logs so that we can filter out warnings based on their prefix. This also turns the remaining two monitorCodeUse callers into warnings. We'll probably still use monitorCodeUse until we know if we want to deprecate but most releases should only have warnings.
75 lines
1.8 KiB
JavaScript
75 lines
1.8 KiB
JavaScript
/**
|
|
* Copyright 2013-2015, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*
|
|
* @providesModule ReactContext
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
var assign = require('Object.assign');
|
|
var emptyObject = require('emptyObject');
|
|
var warning = require('warning');
|
|
|
|
var didWarn = false;
|
|
|
|
/**
|
|
* Keeps track of the current context.
|
|
*
|
|
* The context is automatically passed down the component ownership hierarchy
|
|
* and is accessible via `this.context` on ReactCompositeComponents.
|
|
*/
|
|
var ReactContext = {
|
|
|
|
/**
|
|
* @internal
|
|
* @type {object}
|
|
*/
|
|
current: emptyObject,
|
|
|
|
/**
|
|
* Temporarily extends the current context while executing scopedCallback.
|
|
*
|
|
* A typical use case might look like
|
|
*
|
|
* render: function() {
|
|
* var children = ReactContext.withContext({foo: 'foo'}, () => (
|
|
*
|
|
* ));
|
|
* return <div>{children}</div>;
|
|
* }
|
|
*
|
|
* @param {object} newContext New context to merge into the existing context
|
|
* @param {function} scopedCallback Callback to run with the new context
|
|
* @return {ReactComponent|array<ReactComponent>}
|
|
*/
|
|
withContext: function(newContext, scopedCallback) {
|
|
if (__DEV__) {
|
|
warning(
|
|
didWarn,
|
|
'withContext is deprecated and will be removed in a future version. ' +
|
|
'Use a wrapper component with getChildContext instead.'
|
|
);
|
|
|
|
didWarn = true;
|
|
}
|
|
|
|
var result;
|
|
var previousContext = ReactContext.current;
|
|
ReactContext.current = assign({}, previousContext, newContext);
|
|
try {
|
|
result = scopedCallback();
|
|
} finally {
|
|
ReactContext.current = previousContext;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
};
|
|
|
|
module.exports = ReactContext;
|