@@ -37,6 +37,7 @@ import {
3737 deletedTreeCleanUpLevel ,
3838 enableSuspenseLayoutEffectSemantics ,
3939 enableUpdaterTracking ,
40+ warnAboutCallbackRefReturningFunction ,
4041} from 'shared/ReactFeatureFlags' ;
4142import {
4243 FunctionComponent ,
@@ -250,6 +251,7 @@ function safelyDetachRef(current: Fiber, nearestMountedAncestor: Fiber | null) {
250251 const ref = current . ref ;
251252 if ( ref !== null ) {
252253 if ( typeof ref === 'function' ) {
254+ let retVal ;
253255 try {
254256 if (
255257 enableProfilerTimer &&
@@ -258,17 +260,29 @@ function safelyDetachRef(current: Fiber, nearestMountedAncestor: Fiber | null) {
258260 ) {
259261 try {
260262 startLayoutEffectTimer ( ) ;
261- ref ( null ) ;
263+ retVal = ref ( null ) ;
262264 } finally {
263265 recordLayoutEffectDuration ( current ) ;
264266 }
265267 } else {
266- ref ( null ) ;
268+ retVal = ref ( null ) ;
267269 }
268270 } catch ( error ) {
269271 reportUncaughtErrorInDEV ( error ) ;
270272 captureCommitPhaseError ( current , nearestMountedAncestor , error ) ;
271273 }
274+ if ( __DEV__ ) {
275+ if (
276+ warnAboutCallbackRefReturningFunction &&
277+ typeof retVal === 'function'
278+ ) {
279+ console . error (
280+ 'Unexpected return value from a callback ref in %s. ' +
281+ 'A callback ref should not return a function.' ,
282+ getComponentNameFromFiber ( current ) ,
283+ ) ;
284+ }
285+ }
272286 } else {
273287 ref . current = null ;
274288 }
@@ -1077,19 +1091,32 @@ function commitAttachRef(finishedWork: Fiber) {
10771091 instanceToUse = instance ;
10781092 }
10791093 if ( typeof ref === 'function' ) {
1094+ let retVal ;
10801095 if (
10811096 enableProfilerTimer &&
10821097 enableProfilerCommitHooks &&
10831098 finishedWork . mode & ProfileMode
10841099 ) {
10851100 try {
10861101 startLayoutEffectTimer ( ) ;
1087- ref ( instanceToUse ) ;
1102+ retVal = ref ( instanceToUse ) ;
10881103 } finally {
10891104 recordLayoutEffectDuration ( finishedWork ) ;
10901105 }
10911106 } else {
1092- ref ( instanceToUse ) ;
1107+ retVal = ref ( instanceToUse ) ;
1108+ }
1109+ if ( __DEV__ ) {
1110+ if (
1111+ warnAboutCallbackRefReturningFunction &&
1112+ typeof retVal === 'function'
1113+ ) {
1114+ console . error (
1115+ 'Unexpected return value from a callback ref in %s. ' +
1116+ 'A callback ref should not return a function.' ,
1117+ getComponentNameFromFiber ( finishedWork ) ,
1118+ ) ;
1119+ }
10931120 }
10941121 } else {
10951122 if ( __DEV__ ) {
0 commit comments