@@ -9,6 +9,7 @@ import invariant from 'shared/invariant';
99import { REACT_ELEMENT_TYPE } from 'shared/ReactSymbols' ;
1010
1111import ReactCurrentOwner from './ReactCurrentOwner' ;
12+ import getComponentName from 'shared/getComponentName' ;
1213
1314const hasOwnProperty = Object . prototype . hasOwnProperty ;
1415
@@ -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,31 @@ function defineRefPropWarningGetter(props, displayName) {
8996 } ) ;
9097}
9198
99+ function warnIfStringRefCannotBeAutoConverted ( config ) {
100+ if (
101+ typeof config . ref === 'string' &&
102+ ReactCurrentOwner . current &&
103+ config . __self &&
104+ ReactCurrentOwner . current . stateNode !== config . __self
105+ ) {
106+ const componentName = getComponentName ( ReactCurrentOwner . current . type ) ;
107+
108+ if ( ! didWarnAboutStringRefs [ componentName ] ) {
109+ console . error (
110+ 'Component "%s" contains the string ref "%s". ' +
111+ 'Support for string refs will be removed in a future major release. ' +
112+ 'This case cannot be automatically converted to an arrow function. ' +
113+ 'We as you to manually fix this case by using useRef() or createRef() instead. ' +
114+ 'Learn more about using refs safely here: ' +
115+ 'https://fb.me/react-strict-mode-string-ref' ,
116+ getComponentName ( ReactCurrentOwner . current . type ) ,
117+ config . ref ,
118+ ) ;
119+ didWarnAboutStringRefs [ componentName ] = true ;
120+ }
121+ }
122+ }
123+
92124/**
93125 * Factory method to create a new React element. This no longer adheres to
94126 * the class pattern, so do not use new to call it. Also, instanceof check
@@ -260,6 +292,7 @@ export function jsxDEV(type, config, maybeKey, source, self) {
260292
261293 if ( hasValidRef ( config ) ) {
262294 ref = config . ref ;
295+ warnIfStringRefCannotBeAutoConverted ( config ) ;
263296 }
264297
265298 // Remaining properties are added to a new props object
@@ -324,6 +357,9 @@ export function createElement(type, config, children) {
324357 if ( config != null ) {
325358 if ( hasValidRef ( config ) ) {
326359 ref = config . ref ;
360+ if ( __DEV__ ) {
361+ warnIfStringRefCannotBeAutoConverted ( config ) ;
362+ }
327363 }
328364 if ( hasValidKey ( config ) ) {
329365 key = '' + config . key ;
0 commit comments