File tree 2 files changed +42
-0
lines changed
2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,37 @@ describe('ReactDOMRoot', () => {
51
51
expect ( callback ) . not . toHaveBeenCalled ( ) ;
52
52
} ) ;
53
53
54
+ it ( 'warn if a container is passed to root.render(...)' , async ( ) => {
55
+ function App ( ) {
56
+ return 'Child' ;
57
+ }
58
+
59
+ const root = ReactDOM . createRoot ( container ) ;
60
+ expect ( ( ) => root . render ( < App /> , { } ) ) . toErrorDev (
61
+ 'You passed a second argument to root.render(...) but it only accepts ' +
62
+ 'one argument.' ,
63
+ {
64
+ withoutStack : true ,
65
+ } ,
66
+ ) ;
67
+ } ) ;
68
+
69
+ it ( 'warn if a container is passed to root.render(...)' , async ( ) => {
70
+ function App ( ) {
71
+ return 'Child' ;
72
+ }
73
+
74
+ const root = ReactDOM . createRoot ( container ) ;
75
+ expect ( ( ) => root . render ( < App /> , container ) ) . toErrorDev (
76
+ 'You passed a container to the second argument of root.render(...). ' +
77
+ "You don't need to pass it again since you already passed it to create " +
78
+ 'the root.' ,
79
+ {
80
+ withoutStack : true ,
81
+ } ,
82
+ ) ;
83
+ } ) ;
84
+
54
85
it ( 'warns if a callback parameter is provided to unmount' , ( ) => {
55
86
const callback = jest . fn ( ) ;
56
87
const root = ReactDOM . createRoot ( container ) ;
Original file line number Diff line number Diff line change @@ -104,7 +104,18 @@ ReactDOMHydrationRoot.prototype.render = ReactDOMRoot.prototype.render = functio
104
104
'render(...): does not support the second callback argument. ' +
105
105
'To execute a side effect after rendering, declare it in a component body with useEffect().' ,
106
106
) ;
107
+ } else if ( isValidContainer ( arguments [ 1 ] ) ) {
108
+ console . error (
109
+ 'You passed a container to the second argument of root.render(...). ' +
110
+ "You don't need to pass it again since you already passed it to create the root." ,
111
+ ) ;
112
+ } else if ( typeof arguments [ 1 ] !== 'undefined' ) {
113
+ console . error (
114
+ 'You passed a second argument to root.render(...) but it only accepts ' +
115
+ 'one argument.' ,
116
+ ) ;
107
117
}
118
+
108
119
const container = root . containerInfo ;
109
120
110
121
if ( container . nodeType !== COMMENT_NODE ) {
You can’t perform that action at this time.
0 commit comments