Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81152 views
1
/**
2
* Copyright 2013-2014, 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
16
/**
17
* Keeps track of the current context.
18
*
19
* The context is automatically passed down the component ownership hierarchy
20
* and is accessible via `this.context` on ReactCompositeComponents.
21
*/
22
var ReactContext = {
23
24
/**
25
* @internal
26
* @type {object}
27
*/
28
current: {},
29
30
/**
31
* Temporarily extends the current context while executing scopedCallback.
32
*
33
* A typical use case might look like
34
*
35
* render: function() {
36
* var children = ReactContext.withContext({foo: 'foo'}, () => (
37
*
38
* ));
39
* return <div>{children}</div>;
40
* }
41
*
42
* @param {object} newContext New context to merge into the existing context
43
* @param {function} scopedCallback Callback to run with the new context
44
* @return {ReactComponent|array<ReactComponent>}
45
*/
46
withContext: function(newContext, scopedCallback) {
47
var result;
48
var previousContext = ReactContext.current;
49
ReactContext.current = assign({}, previousContext, newContext);
50
try {
51
result = scopedCallback();
52
} finally {
53
ReactContext.current = previousContext;
54
}
55
return result;
56
}
57
58
};
59
60
module.exports = ReactContext;
61
62