Warn when multiple instances of React are loaded on the same page
This causes a variety of hard-to-debug issues. See #2402 for examples. Fixes #2402
Robert Knight committed
Apr 3, 2015 at 10:56 UTC
8ad58ae45a4fd3a5def5732fc377f1351950c4dd
2 files changed
+26
-5
grunt/tasks/version-check.js
+1
-1
@@ -6,7 +6,7 @@ var grunt = require('grunt');
6
// Check that the version we're exporting is the same one we expect in the
7
// package. This is not an ideal way to do this, but makes sure that we keep
8
// them in sync.
9
-var reactVersionExp = /\bReact\.version\s*=\s*['"]([^'"]+)['"];/;
9
+var reactVersionExp = /\bREACT_VERSION\s*=\s*['"]([^'"]+)['"];/;
10
11
module.exports = function() {
12
var reactVersion = reactVersionExp.exec(
src/browser/ui/React.js
+25
-4
@@ -13,6 +13,30 @@
13
14
'use strict';
15
16
+var REACT_VERSION = '0.14.0-alpha';
17
+
18
+var ExecutionEnvironment = require('ExecutionEnvironment');
19
+var warning = require('warning');
20
+
21
+if (__DEV__) {
22
+ // before performing any initialization of React, check that
23
+ // only one instance is in use on the current page.
24
+ //
25
+ // Using multiple instances cause a variety of problems if elements
26
+ // from different versions are mixed on the same page.
27
+ //
28
+ // See issue #2402
29
+ if (ExecutionEnvironment.canUseDOM) {
30
+ warning(
31
+ typeof window.__REACT_VERSION__ === 'undefined',
32
+ 'Multiple instances of React have been initialized on the same page. ' +
33
+ 'Currently initializing React v' + REACT_VERSION + ' but another instance of React v' +
34
+ window.__REACT_VERSION__ + ' was already initialized'
35
+ );
36
+ window.__REACT_VERSION__ = REACT_VERSION;
37
+ }
38
+}
39
+
40
var ReactChildren = require('ReactChildren');
41
var ReactComponent = require('ReactComponent');
42
var ReactClass = require('ReactClass');
@@ -33,7 +57,6 @@ var ReactServerRendering = require('ReactServerRendering');
57
var assign = require('Object.assign');
58
var findDOMNode = require('findDOMNode');
59
var onlyChild = require('onlyChild');
36
-var warning = require('warning');
60
61
ReactDefaultInjection.inject();
62
@@ -50,6 +73,7 @@ if (__DEV__) {
73
var render = ReactPerf.measure('React', 'render', ReactMount.render);
74
75
var React = {
76
+ version: REACT_VERSION,
77
Children: {
78
map: ReactChildren.map,
79
forEach: ReactChildren.forEach,
@@ -96,7 +120,6 @@ if (
120
}
121
122
if (__DEV__) {
99
- var ExecutionEnvironment = require('ExecutionEnvironment');
123
if (ExecutionEnvironment.canUseDOM && window.top === window.self) {
124
125
// If we're in Chrome, look for the devtools marker and provide a download
@@ -152,6 +175,4 @@ if (__DEV__) {
175
}
176
}
177
155
-React.version = '0.14.0-alpha';
156
-
178
module.exports = React;