Add an invariant to createRoot() to validate containers (#13279)
Dan Abramov committed
Jul 27, 2018 at 16:50 UTC
840cb1a2683dd7c0f3beafd094081d16b636721e
2 files changed
+12
packages/react-dom/src/__tests__/ReactDOMRoot-test.js
+8
@@ -366,4 +366,12 @@ describe('ReactDOMRoot', () => {
366
);
367
expect(() => batch.commit()).toThrow('Element type is invalid');
368
});
369
+
370
+ it('throws a good message on invalid containers', () => {
371
+ expect(() => {
372
+ ReactDOM.unstable_createRoot(<div>Hi</div>);
373
+ }).toThrow(
374
+ 'unstable_createRoot(...): Target container is not a DOM element.',
375
+ );
376
+ });
377
});
packages/react-dom/src/client/ReactDOM.js
+4
@@ -758,6 +758,10 @@ ReactDOM.unstable_createRoot = function createRoot(
758
container: DOMContainer,
759
options?: RootOptions,
760
): ReactRoot {
761
+ invariant(
762
+ isValidContainer(container),
763
+ 'unstable_createRoot(...): Target container is not a DOM element.',
764
+ );
765
const hydrate = options != null && options.hydrate === true;
766
return new ReactRoot(container, true, hydrate);
767
};