Skip to content

Commit

Permalink
Warn when multiple instances of React are loaded on the same page
Browse files Browse the repository at this point in the history
This causes a variety of hard-to-debug issues.
See facebook#2402 for examples.

Fixes facebook#2402
  • Loading branch information
Robert Knight committed Apr 3, 2015
1 parent 5a3bda9 commit dd0d5e3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion grunt/tasks/version-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var grunt = require('grunt');
// Check that the version we're exporting is the same one we expect in the
// package. This is not an ideal way to do this, but makes sure that we keep
// them in sync.
var reactVersionExp = /\bReact\.version\s*=\s*['"]([^'"]+)['"];/;
var reactVersionExp = /\bREACT_VERSION\s*=\s*['"]([^'"]+)['"];/;

module.exports = function() {
var reactVersion = reactVersionExp.exec(
Expand Down
28 changes: 25 additions & 3 deletions src/browser/ui/React.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,30 @@

'use strict';

var REACT_VERSION = '0.14.0-alpha';

var warning = require('warning');

if (__DEV__) {
// before performing any initialization of React, check that
// only one instance is in use on the current page.
//
// Using multiple instances cause a variety of problems if elements
// from different versions are mixed on the same page.
//
// See issue #2402
var ExecutionEnvironment = require('ExecutionEnvironment');
if (ExecutionEnvironment.canUseDOM) {
warning(
typeof window.__REACT_VERSION__ === 'undefined',
'Multiple instances of React have been initialized on the same page. ' +
'Currently initializing React v' + REACT_VERSION + ' but another instance of React v' +
window.__REACT_VERSION__ + ' was already initialized'
);
window.__REACT_VERSION__ = REACT_VERSION;
}
}

var ReactChildren = require('ReactChildren');
var ReactComponent = require('ReactComponent');
var ReactClass = require('ReactClass');
Expand All @@ -33,7 +57,6 @@ var ReactServerRendering = require('ReactServerRendering');
var assign = require('Object.assign');
var findDOMNode = require('findDOMNode');
var onlyChild = require('onlyChild');
var warning = require('warning');

ReactDefaultInjection.inject();

Expand All @@ -50,6 +73,7 @@ if (__DEV__) {
var render = ReactPerf.measure('React', 'render', ReactMount.render);

var React = {
version: REACT_VERSION,
Children: {
map: ReactChildren.map,
forEach: ReactChildren.forEach,
Expand Down Expand Up @@ -152,6 +176,4 @@ if (__DEV__) {
}
}

React.version = '0.14.0-alpha';

module.exports = React;

0 comments on commit dd0d5e3

Please sign in to comment.