@@ -30,6 +30,7 @@ import {
3030import invariant from 'shared/invariant' ;
3131import warning from 'shared/warning' ;
3232import warningWithoutStack from 'shared/warningWithoutStack' ;
33+ import { warnAboutStringRefs } from 'shared/ReactFeatureFlags' ;
3334
3435import {
3536 createWorkInProgress ,
@@ -49,15 +50,15 @@ import {StrictMode} from './ReactTypeOfMode';
4950
5051let didWarnAboutMaps ;
5152let didWarnAboutGenerators ;
52- let didWarnAboutStringRefInStrictMode ;
53+ let didWarnAboutStringRefs ;
5354let ownerHasKeyUseWarning ;
5455let ownerHasFunctionTypeWarning ;
5556let warnForMissingKey = ( child : mixed ) => { } ;
5657
5758if ( __DEV__ ) {
5859 didWarnAboutMaps = false ;
5960 didWarnAboutGenerators = false ;
60- didWarnAboutStringRefInStrictMode = { } ;
61+ didWarnAboutStringRefs = { } ;
6162
6263 /**
6364 * Warn if there's no key explicitly set on dynamic arrays of children or
@@ -114,21 +115,38 @@ function coerceRef(
114115 typeof mixedRef !== 'object'
115116 ) {
116117 if ( __DEV__ ) {
117- if ( returnFiber . mode & StrictMode ) {
118+ // TODO: Clean this up once we turn on the string ref warning for
119+ // everyone, because the strict mode case will no longer be relevant
120+ if ( returnFiber . mode & StrictMode || warnAboutStringRefs ) {
118121 const componentName = getComponentName ( returnFiber . type ) || 'Component' ;
119- if ( ! didWarnAboutStringRefInStrictMode [ componentName ] ) {
120- warningWithoutStack (
121- false ,
122- 'A string ref, "%s", has been found within a strict mode tree. ' +
123- 'String refs are a source of potential bugs and should be avoided. ' +
124- 'We recommend using createRef() instead.' +
125- '\n%s' +
126- '\n\nLearn more about using refs safely here:' +
127- '\nhttps://fb.me/react-strict-mode-string-ref' ,
128- mixedRef ,
129- getStackByFiberInDevAndProd ( returnFiber ) ,
130- ) ;
131- didWarnAboutStringRefInStrictMode [ componentName ] = true ;
122+ if ( ! didWarnAboutStringRefs [ componentName ] ) {
123+ if ( warnAboutStringRefs ) {
124+ warningWithoutStack (
125+ false ,
126+ 'Component "%s" contains the string ref "%s". Support for string refs ' +
127+ 'will be removed in a future major release. We recommend using ' +
128+ 'useRef() or createRef() instead.' +
129+ '\n%s' +
130+ '\n\nLearn more about using refs safely here:' +
131+ '\nhttps://fb.me/react-strict-mode-string-ref' ,
132+ componentName ,
133+ mixedRef ,
134+ getStackByFiberInDevAndProd ( returnFiber ) ,
135+ ) ;
136+ } else {
137+ warningWithoutStack (
138+ false ,
139+ 'A string ref, "%s", has been found within a strict mode tree. ' +
140+ 'String refs are a source of potential bugs and should be avoided. ' +
141+ 'We recommend using useRef() or createRef() instead.' +
142+ '\n%s' +
143+ '\n\nLearn more about using refs safely here:' +
144+ '\nhttps://fb.me/react-strict-mode-string-ref' ,
145+ mixedRef ,
146+ getStackByFiberInDevAndProd ( returnFiber ) ,
147+ ) ;
148+ }
149+ didWarnAboutStringRefs [ componentName ] = true ;
132150 }
133151 }
134152 }
0 commit comments