@@ -33,7 +33,13 @@ import {
3333 REACT_LAZY_TYPE ,
3434 REACT_CONTEXT_TYPE ,
3535} from 'shared/ReactSymbols' ;
36- import { ClassComponent , HostText , HostPortal , Fragment } from './ReactWorkTags' ;
36+ import {
37+ ClassComponent ,
38+ HostRoot ,
39+ HostText ,
40+ HostPortal ,
41+ Fragment ,
42+ } from './ReactWorkTags' ;
3743import isArray from 'shared/isArray' ;
3844import { checkPropStringCoercion } from 'shared/CheckStringCoercion' ;
3945
@@ -79,6 +85,7 @@ let didWarnAboutGenerators;
7985let didWarnAboutStringRefs ;
8086let ownerHasKeyUseWarning ;
8187let ownerHasFunctionTypeWarning ;
88+ let ownerHasSymbolTypeWarning ;
8289let warnForMissingKey = ( child : mixed , returnFiber : Fiber ) => { } ;
8390
8491if ( __DEV__ ) {
@@ -93,6 +100,7 @@ if (__DEV__) {
93100 */
94101 ownerHasKeyUseWarning = ( { } : { [ string ] : boolean } ) ;
95102 ownerHasFunctionTypeWarning = ( { } : { [ string ] : boolean } ) ;
103+ ownerHasSymbolTypeWarning = ( { } : { [ string ] : boolean } ) ;
96104
97105 warnForMissingKey = ( child : mixed , returnFiber : Fiber ) => {
98106 if ( child === null || typeof child !== 'object' ) {
@@ -267,20 +275,68 @@ function throwOnInvalidObjectType(returnFiber: Fiber, newChild: Object) {
267275 ) ;
268276}
269277
270- function warnOnFunctionType ( returnFiber : Fiber ) {
278+ function warnOnFunctionType ( returnFiber : Fiber , invalidChild : Function ) {
271279 if ( __DEV__ ) {
272- const componentName = getComponentNameFromFiber ( returnFiber ) || 'Component' ;
280+ const parentName = getComponentNameFromFiber ( returnFiber ) || 'Component' ;
273281
274- if ( ownerHasFunctionTypeWarning [ componentName ] ) {
282+ if ( ownerHasFunctionTypeWarning [ parentName ] ) {
275283 return ;
276284 }
277- ownerHasFunctionTypeWarning [ componentName ] = true ;
285+ ownerHasFunctionTypeWarning [ parentName ] = true ;
286+
287+ const name = invalidChild . displayName || invalidChild . name || 'Component' ;
288+
289+ if ( returnFiber . tag === HostRoot ) {
290+ console . error (
291+ 'Functions are not valid as a React child. This may happen if ' +
292+ 'you return %s instead of <%s /> from render. ' +
293+ 'Or maybe you meant to call this function rather than return it.\n' +
294+ ' root.render(%s)' ,
295+ name ,
296+ name ,
297+ name ,
298+ ) ;
299+ } else {
300+ console . error (
301+ 'Functions are not valid as a React child. This may happen if ' +
302+ 'you return %s instead of <%s /> from render. ' +
303+ 'Or maybe you meant to call this function rather than return it.\n' +
304+ ' <%s>{%s}</%s>' ,
305+ name ,
306+ name ,
307+ parentName ,
308+ name ,
309+ parentName ,
310+ ) ;
311+ }
312+ }
313+ }
278314
279- console . error (
280- 'Functions are not valid as a React child. This may happen if ' +
281- 'you return a Component instead of <Component /> from render. ' +
282- 'Or maybe you meant to call this function rather than return it.' ,
283- ) ;
315+ function warnOnSymbolType ( returnFiber : Fiber , invalidChild : symbol ) {
316+ if ( __DEV__ ) {
317+ const parentName = getComponentNameFromFiber ( returnFiber ) || 'Component' ;
318+
319+ if ( ownerHasSymbolTypeWarning [ parentName ] ) {
320+ return ;
321+ }
322+ ownerHasSymbolTypeWarning [ parentName ] = true ;
323+
324+ // eslint-disable-next-line react-internal/safe-string-coercion
325+ const name = String ( invalidChild ) ;
326+
327+ if ( returnFiber . tag === HostRoot ) {
328+ console . error (
329+ 'Symbols are not valid as a React child.\n' + ' root.render(%s)' ,
330+ name ,
331+ ) ;
332+ } else {
333+ console . error (
334+ 'Symbols are not valid as a React child.\n' + ' <%s>%s</%s>' ,
335+ parentName ,
336+ name ,
337+ parentName ,
338+ ) ;
339+ }
284340 }
285341}
286342
@@ -656,7 +712,10 @@ function createChildReconciler(
656712
657713 if ( __DEV__ ) {
658714 if ( typeof newChild === 'function' ) {
659- warnOnFunctionType ( returnFiber ) ;
715+ warnOnFunctionType ( returnFiber , newChild ) ;
716+ }
717+ if ( typeof newChild === 'symbol' ) {
718+ warnOnSymbolType ( returnFiber , newChild ) ;
660719 }
661720 }
662721
@@ -778,7 +837,10 @@ function createChildReconciler(
778837
779838 if ( __DEV__ ) {
780839 if ( typeof newChild === 'function' ) {
781- warnOnFunctionType ( returnFiber ) ;
840+ warnOnFunctionType ( returnFiber , newChild ) ;
841+ }
842+ if ( typeof newChild === 'symbol' ) {
843+ warnOnSymbolType ( returnFiber , newChild ) ;
782844 }
783845 }
784846
@@ -894,7 +956,10 @@ function createChildReconciler(
894956
895957 if ( __DEV__ ) {
896958 if ( typeof newChild === 'function' ) {
897- warnOnFunctionType ( returnFiber ) ;
959+ warnOnFunctionType ( returnFiber , newChild ) ;
960+ }
961+ if ( typeof newChild === 'symbol' ) {
962+ warnOnSymbolType ( returnFiber , newChild ) ;
898963 }
899964 }
900965
@@ -1621,7 +1686,10 @@ function createChildReconciler(
16211686
16221687 if ( __DEV__ ) {
16231688 if ( typeof newChild === 'function' ) {
1624- warnOnFunctionType ( returnFiber ) ;
1689+ warnOnFunctionType ( returnFiber , newChild ) ;
1690+ }
1691+ if ( typeof newChild === 'symbol' ) {
1692+ warnOnSymbolType ( returnFiber , newChild ) ;
16251693 }
16261694 }
16271695
0 commit comments