5
5
* LICENSE file in the root directory of this source tree.
6
6
*/
7
7
8
+ import getComponentName from 'shared/getComponentName' ;
8
9
import invariant from 'shared/invariant' ;
9
10
import { REACT_ELEMENT_TYPE } from 'shared/ReactSymbols' ;
10
11
@@ -19,7 +20,13 @@ const RESERVED_PROPS = {
19
20
__source : true ,
20
21
} ;
21
22
22
- let specialPropKeyWarningShown , specialPropRefWarningShown ;
23
+ let specialPropKeyWarningShown ,
24
+ specialPropRefWarningShown ,
25
+ didWarnAboutStringRefs ;
26
+
27
+ if ( __DEV__ ) {
28
+ didWarnAboutStringRefs = { } ;
29
+ }
23
30
24
31
function hasValidRef ( config ) {
25
32
if ( __DEV__ ) {
@@ -89,6 +96,33 @@ function defineRefPropWarningGetter(props, displayName) {
89
96
} ) ;
90
97
}
91
98
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 as 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
+
92
126
/**
93
127
* Factory method to create a new React element. This no longer adheres to
94
128
* 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) {
260
294
261
295
if ( hasValidRef ( config ) ) {
262
296
ref = config . ref ;
297
+ warnIfStringRefCannotBeAutoConverted ( config ) ;
263
298
}
264
299
265
300
// Remaining properties are added to a new props object
@@ -324,6 +359,10 @@ export function createElement(type, config, children) {
324
359
if ( config != null ) {
325
360
if ( hasValidRef ( config ) ) {
326
361
ref = config . ref ;
362
+
363
+ if ( __DEV__ ) {
364
+ warnIfStringRefCannotBeAutoConverted ( config ) ;
365
+ }
327
366
}
328
367
if ( hasValidKey ( config ) ) {
329
368
key = '' + config . key ;
0 commit comments