Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80517 views
1
/**
2
* Copyright 2013-2015, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule ReactContext
10
*/
11
12
'use strict';
13
14
var assign = require("./Object.assign");
15
var emptyObject = require("./emptyObject");
16
var warning = require("./warning");
17
18
var didWarn = false;
19
20
/**
21
* Keeps track of the current context.
22
*
23
* The context is automatically passed down the component ownership hierarchy
24
* and is accessible via `this.context` on ReactCompositeComponents.
25
*/
26
var ReactContext = {
27
28
/**
29
* @internal
30
* @type {object}
31
*/
32
current: emptyObject,
33
34
/**
35
* Temporarily extends the current context while executing scopedCallback.
36
*
37
* A typical use case might look like
38
*
39
* render: function() {
40
* var children = ReactContext.withContext({foo: 'foo'}, () => (
41
*
42
* ));
43
* return <div>{children}</div>;
44
* }
45
*
46
* @param {object} newContext New context to merge into the existing context
47
* @param {function} scopedCallback Callback to run with the new context
48
* @return {ReactComponent|array<ReactComponent>}
49
*/
50
withContext: function(newContext, scopedCallback) {
51
if ("production" !== process.env.NODE_ENV) {
52
("production" !== process.env.NODE_ENV ? warning(
53
didWarn,
54
'withContext is deprecated and will be removed in a future version. ' +
55
'Use a wrapper component with getChildContext instead.'
56
) : null);
57
58
didWarn = true;
59
}
60
61
var result;
62
var previousContext = ReactContext.current;
63
ReactContext.current = assign({}, previousContext, newContext);
64
try {
65
result = scopedCallback();
66
} finally {
67
ReactContext.current = previousContext;
68
}
69
return result;
70
}
71
72
};
73
74
module.exports = ReactContext;
75
76