Skip to content

Commit b5bea2a

Browse files
committed
Warn when nesting 15 subtree inside 16
1 parent 4d08914 commit b5bea2a

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/renderers/dom/shared/__tests__/renderSubtreeIntoContainer-test.js

+24
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
var React = require('react');
1515
var PropTypes = require('prop-types');
1616
var ReactDOM = require('react-dom');
17+
var ReactDOMFeatureFlags = require('ReactDOMFeatureFlags');
1718
var ReactTestUtils = require('react-dom/test-utils');
1819
var renderSubtreeIntoContainer = require('react-dom')
1920
.unstable_renderSubtreeIntoContainer;
@@ -299,4 +300,27 @@ describe('renderSubtreeIntoContainer', () => {
299300
ReactDOM.render(<Parent value="foo" />, container);
300301
expect(portal2.textContent).toBe('foo');
301302
});
303+
304+
if (ReactDOMFeatureFlags.useFiber) {
305+
it('fails gracefully when mixing React 15 and 16', () => {
306+
class C extends React.Component {
307+
render() {
308+
return <div />;
309+
}
310+
}
311+
const c = ReactDOM.render(<C />, document.createElement('div'));
312+
// React 15 calls this:
313+
// https://github.com/facebook/react/blob/77b71fc3c4/src/renderers/dom/client/ReactMount.js#L478-L479
314+
expect(() => {
315+
c._reactInternalInstance._processChildContext({});
316+
}).toThrow(
317+
'_processChildContext is not available in React 16+. This likely ' +
318+
'means you have multiple copies of React and are attempting to nest ' +
319+
'a React 15 tree inside a React 16 tree using ' +
320+
"unstable_renderSubtreeIntoContainer, which isn't supported. Try to " +
321+
'make sure you have only one copy of React (and ideally, switch to ' +
322+
'React.unstable_createPortal).',
323+
);
324+
});
325+
}
302326
});

src/renderers/shared/fiber/ReactFiber.js

+22
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,28 @@ function FiberNode(
207207
}
208208
}
209209

210+
if (__DEV__) {
211+
// This is so gross but it's at least non-critical and can be removed if it
212+
// causes problems. This is meant to give a nicer error message in the case of
213+
// calling ReactDOM15.unstable_renderSubtreeIntoContainer(reactDOM16Component,
214+
// ...)) which otherwise throws a "_processChildContext is not a function"
215+
// exception.
216+
Object.defineProperty(FiberNode.prototype, '_processChildContext', {
217+
enumerable: false,
218+
value: function() {
219+
invariant(
220+
false,
221+
'_processChildContext is not available in React 16+. This likely ' +
222+
'means you have multiple copies of React and are attempting to nest ' +
223+
'a React 15 tree inside a React 16 tree using ' +
224+
"unstable_renderSubtreeIntoContainer, which isn't supported. Try " +
225+
'to make sure you have only one copy of React (and ideally, switch ' +
226+
'to React.unstable_createPortal).',
227+
);
228+
},
229+
});
230+
}
231+
210232
// This is a constructor function, rather than a POJO constructor, still
211233
// please ensure we do the following:
212234
// 1) Nobody should add any instance methods on this. Instance methods can be

0 commit comments

Comments
 (0)