Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81155 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 ReactComponentBrowserEnvironment
10
*/
11
12
/*jslint evil: true */
13
14
"use strict";
15
16
var ReactDOMIDOperations = require('ReactDOMIDOperations');
17
var ReactMarkupChecksum = require('ReactMarkupChecksum');
18
var ReactMount = require('ReactMount');
19
var ReactPerf = require('ReactPerf');
20
var ReactReconcileTransaction = require('ReactReconcileTransaction');
21
22
var getReactRootElementInContainer = require('getReactRootElementInContainer');
23
var invariant = require('invariant');
24
var setInnerHTML = require('setInnerHTML');
25
26
27
var ELEMENT_NODE_TYPE = 1;
28
var DOC_NODE_TYPE = 9;
29
30
31
/**
32
* Abstracts away all functionality of `ReactComponent` requires knowledge of
33
* the browser context.
34
*/
35
var ReactComponentBrowserEnvironment = {
36
ReactReconcileTransaction: ReactReconcileTransaction,
37
38
BackendIDOperations: ReactDOMIDOperations,
39
40
/**
41
* If a particular environment requires that some resources be cleaned up,
42
* specify this in the injected Mixin. In the DOM, we would likely want to
43
* purge any cached node ID lookups.
44
*
45
* @private
46
*/
47
unmountIDFromEnvironment: function(rootNodeID) {
48
ReactMount.purgeID(rootNodeID);
49
},
50
51
/**
52
* @param {string} markup Markup string to place into the DOM Element.
53
* @param {DOMElement} container DOM Element to insert markup into.
54
* @param {boolean} shouldReuseMarkup Should reuse the existing markup in the
55
* container if possible.
56
*/
57
mountImageIntoNode: ReactPerf.measure(
58
'ReactComponentBrowserEnvironment',
59
'mountImageIntoNode',
60
function(markup, container, shouldReuseMarkup) {
61
invariant(
62
container && (
63
container.nodeType === ELEMENT_NODE_TYPE ||
64
container.nodeType === DOC_NODE_TYPE
65
),
66
'mountComponentIntoNode(...): Target container is not valid.'
67
);
68
69
if (shouldReuseMarkup) {
70
if (ReactMarkupChecksum.canReuseMarkup(
71
markup,
72
getReactRootElementInContainer(container))) {
73
return;
74
} else {
75
invariant(
76
container.nodeType !== DOC_NODE_TYPE,
77
'You\'re trying to render a component to the document using ' +
78
'server rendering but the checksum was invalid. This usually ' +
79
'means you rendered a different component type or props on ' +
80
'the client from the one on the server, or your render() ' +
81
'methods are impure. React cannot handle this case due to ' +
82
'cross-browser quirks by rendering at the document root. You ' +
83
'should look for environment dependent code in your components ' +
84
'and ensure the props are the same client and server side.'
85
);
86
87
if (__DEV__) {
88
console.warn(
89
'React attempted to use reuse markup in a container but the ' +
90
'checksum was invalid. This generally means that you are ' +
91
'using server rendering and the markup generated on the ' +
92
'server was not what the client was expecting. React injected ' +
93
'new markup to compensate which works but you have lost many ' +
94
'of the benefits of server rendering. Instead, figure out ' +
95
'why the markup being generated is different on the client ' +
96
'or server.'
97
);
98
}
99
}
100
}
101
102
invariant(
103
container.nodeType !== DOC_NODE_TYPE,
104
'You\'re trying to render a component to the document but ' +
105
'you didn\'t use server rendering. We can\'t do this ' +
106
'without using server rendering due to cross-browser quirks. ' +
107
'See renderComponentToString() for server rendering.'
108
);
109
110
setInnerHTML(container, markup);
111
}
112
)
113
};
114
115
module.exports = ReactComponentBrowserEnvironment;
116
117