Skip to content

Commit

Permalink
Set up proxy to intercept React.DOM usage
Browse files Browse the repository at this point in the history
  • Loading branch information
nhunzaker committed Nov 22, 2016
1 parent e0b512c commit c93cdf2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/addons/__tests__/ReactDOMFactories-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@

'use strict';

var {div} = require('ReactDOMFactories');

describe('ReactDOMFactories', () => {
var React;
var {div} = require('ReactDOMFactories');

beforeEach(function () {
React = require('React');
})

it('allow factories to be called without warnings', () => {
spyOn(console, 'error');
Expand All @@ -22,4 +26,12 @@ describe('ReactDOMFactories', () => {
expectDev(console.error.calls.count()).toBe(0);
});

it('warns once when accessing React.DOM methods', () => {
spyOn(console, 'error');

React.DOM.div;
React.DOM.p;

expectDev(console.error.calls.count()).toBe(1);
});
});
32 changes: 32 additions & 0 deletions src/isomorphic/React.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var ReactChildren = require('ReactChildren');
var ReactComponent = require('ReactComponent');
var ReactPureComponent = require('ReactPureComponent');
var ReactClass = require('ReactClass');
var ReactDOMFactories = require('ReactDOMFactories');
var ReactElement = require('ReactElement');
var ReactPropTypes = require('ReactPropTypes');
var ReactVersion = require('ReactVersion');
Expand Down Expand Up @@ -79,10 +80,41 @@ var React = {
return mixin;
},

// This looks DOM specific but these are actually isomorphic helpers
// since they are just generating DOM strings.
DOM: ReactDOMFactories,

version: ReactVersion,

// Deprecated hook for JSX spread, don't use this for anything.
__spread: __spread,
};

if (__DEV__) {
// React.DOM factories are deprecated, set up a Proxy to listen for
// accesses to this namespace and alert users to switch to the
// associated addon.
if (typeof Proxy === 'function') {
var hasWarnedOfFactories = false;

React.DOM = new Proxy(React.DOM, {
get: function (target, name) {
if (!hasWarnedOfFactories) {
warning(
false,
'Accessing React.DOM factories (like `React.DOM.%s`) is deprecated ' +
'and will be removed in the future. Please use the ' +
'react-addons-dom-factories package instead.',
name
);

hasWarnedOfFactories = true;
}
return target[name];
},
});
}

}

module.exports = React;

0 comments on commit c93cdf2

Please sign in to comment.