@@ -68,7 +68,9 @@ import {
68
68
getNodeFromInstance ,
69
69
getFiberCurrentPropsFromNode ,
70
70
getClosestInstanceFromNode ,
71
+ isContainerMarkedAsRoot ,
71
72
markContainerAsRoot ,
73
+ unmarkContainerAsRoot ,
72
74
} from './ReactDOMComponentTree' ;
73
75
import { restoreControlledState } from './ReactDOMComponent' ;
74
76
import { dispatchEvent } from '../events/ReactDOMEventListener' ;
@@ -174,11 +176,9 @@ setRestoreImplementation(restoreControlledState);
174
176
export type DOMContainer =
175
177
| ( Element & {
176
178
_reactRootContainer : ?_ReactRoot ,
177
- _reactHasBeenPassedToCreateRootDEV : ?boolean ,
178
179
} )
179
180
| ( Document & {
180
181
_reactRootContainer : ?_ReactRoot ,
181
- _reactHasBeenPassedToCreateRootDEV : ?boolean ,
182
182
} ) ;
183
183
184
184
type _ReactRoot = {
@@ -226,22 +226,28 @@ ReactRoot.prototype.render = ReactBlockingRoot.prototype.render = function(
226
226
callback : ?( ) = > mixed ,
227
227
) : void {
228
228
const root = this . _internalRoot ;
229
- callback = callback === undefined ? null : callback ;
229
+ const cb = callback === undefined ? null : callback ;
230
230
if ( __DEV__ ) {
231
- warnOnInvalidCallback ( callback , 'render' ) ;
231
+ warnOnInvalidCallback ( cb , 'render' ) ;
232
232
}
233
- updateContainer ( children , root , null , callback ) ;
233
+ updateContainer ( children , root , null , cb ) ;
234
234
} ;
235
235
236
236
ReactRoot . prototype . unmount = ReactBlockingRoot . prototype . unmount = function (
237
237
callback : ?( ) = > mixed ,
238
238
) : void {
239
239
const root = this . _internalRoot ;
240
- callback = callback === undefined ? null : callback ;
240
+ const cb = callback === undefined ? null : callback ;
241
241
if ( __DEV__ ) {
242
- warnOnInvalidCallback ( callback , 'render' ) ;
242
+ warnOnInvalidCallback ( cb , 'render' ) ;
243
243
}
244
- updateContainer ( null , root , null , callback ) ;
244
+ const container = root . containerInfo ;
245
+ updateContainer ( null , root , null , ( ) => {
246
+ unmarkContainerAsRoot ( container ) ;
247
+ if ( cb !== null ) {
248
+ cb ( ) ;
249
+ }
250
+ } ) ;
245
251
} ;
246
252
247
253
/**
@@ -448,12 +454,17 @@ const ReactDOM: Object = {
448
454
'Target container is not a DOM element.' ,
449
455
) ;
450
456
if ( __DEV__ ) {
451
- warningWithoutStack (
452
- ! container . _reactHasBeenPassedToCreateRootDEV ,
453
- 'You are calling ReactDOM.hydrate() on a container that was previously ' +
454
- 'passed to ReactDOM.createRoot(). This is not supported. ' +
455
- 'Did you mean to call createRoot(container, {hydrate: true}).render(element)?' ,
456
- ) ;
457
+ const isModernRoot =
458
+ isContainerMarkedAsRoot ( container ) &&
459
+ container . _reactRootContainer === undefined ;
460
+ if ( isModernRoot ) {
461
+ warningWithoutStack (
462
+ false ,
463
+ 'You are calling ReactDOM.hydrate() on a container that was previously ' +
464
+ 'passed to ReactDOM.createRoot(). This is not supported. ' +
465
+ 'Did you mean to call createRoot(container, {hydrate: true}).render(element)?' ,
466
+ ) ;
467
+ }
457
468
}
458
469
// TODO: throw or warn if we couldn't hydrate?
459
470
return legacyRenderSubtreeIntoContainer (
@@ -475,12 +486,17 @@ const ReactDOM: Object = {
475
486
'Target container is not a DOM element.' ,
476
487
) ;
477
488
if ( __DEV__ ) {
478
- warningWithoutStack (
479
- ! container . _reactHasBeenPassedToCreateRootDEV ,
480
- 'You are calling ReactDOM.render() on a container that was previously ' +
481
- 'passed to ReactDOM.createRoot(). This is not supported. ' +
482
- 'Did you mean to call root.render(element)?' ,
483
- ) ;
489
+ const isModernRoot =
490
+ isContainerMarkedAsRoot ( container ) &&
491
+ container . _reactRootContainer === undefined ;
492
+ if ( isModernRoot ) {
493
+ warningWithoutStack (
494
+ false ,
495
+ 'You are calling ReactDOM.render() on a container that was previously ' +
496
+ 'passed to ReactDOM.createRoot(). This is not supported. ' +
497
+ 'Did you mean to call root.render(element)?' ,
498
+ ) ;
499
+ }
484
500
}
485
501
return legacyRenderSubtreeIntoContainer (
486
502
null ,
@@ -521,11 +537,16 @@ const ReactDOM: Object = {
521
537
) ;
522
538
523
539
if ( __DEV__ ) {
524
- warningWithoutStack (
525
- ! container . _reactHasBeenPassedToCreateRootDEV ,
526
- 'You are calling ReactDOM.unmountComponentAtNode() on a container that was previously ' +
527
- 'passed to ReactDOM.createRoot(). This is not supported. Did you mean to call root.unmount()?' ,
528
- ) ;
540
+ const isModernRoot =
541
+ isContainerMarkedAsRoot ( container ) &&
542
+ container . _reactRootContainer === undefined ;
543
+ if ( isModernRoot ) {
544
+ warningWithoutStack (
545
+ false ,
546
+ 'You are calling ReactDOM.unmountComponentAtNode() on a container that was previously ' +
547
+ 'passed to ReactDOM.createRoot(). This is not supported. Did you mean to call root.unmount()?' ,
548
+ ) ;
549
+ }
529
550
}
530
551
531
552
if ( container . _reactRootContainer ) {
@@ -543,6 +564,7 @@ const ReactDOM: Object = {
543
564
unbatchedUpdates ( ( ) => {
544
565
legacyRenderSubtreeIntoContainer ( null , null , container , false , ( ) => {
545
566
container . _reactRootContainer = null ;
567
+ unmarkContainerAsRoot ( container ) ;
546
568
} ) ;
547
569
} ) ;
548
570
// If you call unmountComponentAtNode twice in quick succession, you'll
@@ -650,12 +672,22 @@ function createBlockingRoot(
650
672
651
673
function warnIfReactDOMContainerInDEV ( container ) {
652
674
if ( __DEV__ ) {
653
- warningWithoutStack (
654
- ! container . _reactRootContainer ,
655
- 'You are calling ReactDOM.createRoot() on a container that was previously ' +
656
- 'passed to ReactDOM.render(). This is not supported.' ,
657
- ) ;
658
- container . _reactHasBeenPassedToCreateRootDEV = true ;
675
+ if ( isContainerMarkedAsRoot ( container ) ) {
676
+ if ( container . _reactRootContainer ) {
677
+ warningWithoutStack (
678
+ false ,
679
+ 'You are calling ReactDOM.createRoot() on a container that was previously ' +
680
+ 'passed to ReactDOM.render(). This is not supported.' ,
681
+ ) ;
682
+ } else {
683
+ warningWithoutStack (
684
+ false ,
685
+ 'You are calling ReactDOM.createRoot() on a container that ' +
686
+ 'has already been passed to createRoot() before. Instead, call ' +
687
+ 'root.render() on the existing root instead if you want to update it.' ,
688
+ ) ;
689
+ }
690
+ }
659
691
}
660
692
}
661
693
0 commit comments