55 * LICENSE file in the root directory of this source tree.
66 */
77
8+ import getComponentName from 'shared/getComponentName' ;
89import invariant from 'shared/invariant' ;
910import { REACT_ELEMENT_TYPE } from 'shared/ReactSymbols' ;
1011
@@ -19,7 +20,13 @@ const RESERVED_PROPS = {
1920 __source : true ,
2021} ;
2122
22- let specialPropKeyWarningShown , specialPropRefWarningShown ;
23+ let specialPropKeyWarningShown ,
24+ specialPropRefWarningShown ,
25+ didWarnAboutStringRefs ;
26+
27+ if ( __DEV__ ) {
28+ didWarnAboutStringRefs = { } ;
29+ }
2330
2431function hasValidRef ( config ) {
2532 if ( __DEV__ ) {
@@ -89,6 +96,33 @@ function defineRefPropWarningGetter(props, displayName) {
8996 } ) ;
9097}
9198
99+ function warnIfStringRefCannotBeAutoConverted ( config ) {
100+ if ( __DEV__ ) {
101+ if (
102+ typeof config . ref === 'string' &&
103+ ReactCurrentOwner . current &&
104+ config . __self &&
105+ ReactCurrentOwner . current . stateNode !== config . __self
106+ ) {
107+ const componentName = getComponentName ( ReactCurrentOwner . current . type ) ;
108+
109+ if ( ! didWarnAboutStringRefs [ componentName ] ) {
110+ console . error (
111+ 'Component "%s" contains the string ref "%s". ' +
112+ 'Support for string refs will be removed in a future major release. ' +
113+ 'This case cannot be automatically converted to an arrow function. ' +
114+ 'We ask you to manually fix this case by using useRef() or createRef() instead. ' +
115+ 'Learn more about using refs safely here: ' +
116+ 'https://fb.me/react-strict-mode-string-ref' ,
117+ getComponentName ( ReactCurrentOwner . current . type ) ,
118+ config . ref ,
119+ ) ;
120+ didWarnAboutStringRefs [ componentName ] = true ;
121+ }
122+ }
123+ }
124+ }
125+
92126/**
93127 * Factory method to create a new React element. This no longer adheres to
94128 * the class pattern, so do not use new to call it. Also, instanceof check
@@ -260,6 +294,7 @@ export function jsxDEV(type, config, maybeKey, source, self) {
260294
261295 if ( hasValidRef ( config ) ) {
262296 ref = config . ref ;
297+ warnIfStringRefCannotBeAutoConverted ( config ) ;
263298 }
264299
265300 // Remaining properties are added to a new props object
@@ -324,6 +359,10 @@ export function createElement(type, config, children) {
324359 if ( config != null ) {
325360 if ( hasValidRef ( config ) ) {
326361 ref = config . ref ;
362+
363+ if ( __DEV__ ) {
364+ warnIfStringRefCannotBeAutoConverted ( config ) ;
365+ }
327366 }
328367 if ( hasValidKey ( config ) ) {
329368 key = '' + config . key ;
0 commit comments