14
14
var ReactCurrentOwner = require ( 'ReactCurrentOwner' ) ;
15
15
16
16
var assign = require ( 'Object.assign' ) ;
17
+ var warning = require ( 'warning' ) ;
17
18
var canDefineProperty = require ( 'canDefineProperty' ) ;
18
19
19
20
// The Symbol used to tag the ReactElement type. If there is no native Symbol
@@ -29,6 +30,8 @@ var RESERVED_PROPS = {
29
30
__source : true ,
30
31
} ;
31
32
33
+ var specialPropKeyWarningShown , specialPropRefWarningShown ;
34
+
32
35
/**
33
36
* Factory method to create a new React element. This no longer adheres to
34
37
* the class pattern, so do not use new to call it. Also, no instanceof check
@@ -123,8 +126,15 @@ ReactElement.createElement = function(type, config, children) {
123
126
var source = null ;
124
127
125
128
if ( config != null ) {
126
- ref = config . ref === undefined ? null : config . ref ;
127
- key = config . key === undefined ? null : '' + config . key ;
129
+ if ( __DEV__ ) {
130
+ ref = ! config . hasOwnProperty ( 'ref' ) ||
131
+ Object . getOwnPropertyDescriptor ( config , 'ref' ) . get ? null : config . ref ;
132
+ key = ! config . hasOwnProperty ( 'key' ) ||
133
+ Object . getOwnPropertyDescriptor ( config , 'key' ) . get ? null : '' + config . key ;
134
+ } else {
135
+ ref = config . ref === undefined ? null : config . ref ;
136
+ key = config . key === undefined ? null : '' + config . key ;
137
+ }
128
138
self = config . __self === undefined ? null : config . __self ;
129
139
source = config . __source === undefined ? null : config . __source ;
130
140
// Remaining properties are added to a new props object
@@ -158,7 +168,49 @@ ReactElement.createElement = function(type, config, children) {
158
168
}
159
169
}
160
170
}
161
-
171
+ if ( __DEV__ ) {
172
+ // Create dummy `key` and `ref` property to `props` to warn users
173
+ // against its use
174
+ if ( typeof props . $$typeof === 'undefined' ||
175
+ props . $$typeof !== REACT_ELEMENT_TYPE ) {
176
+ if ( ! props . hasOwnProperty ( 'key' ) ) {
177
+ Object . defineProperty ( props , 'key' , {
178
+ get : function ( ) {
179
+ if ( ! specialPropKeyWarningShown ) {
180
+ specialPropKeyWarningShown = true ;
181
+ warning (
182
+ false ,
183
+ '%s: `key` is not a prop. Trying to access it will result ' +
184
+ 'in `undefined` being returned. If you need to access the same ' +
185
+ 'value within the child component, you should pass it as a different ' +
186
+ 'prop. (https://fb.me/react-special-props)' ,
187
+ 'displayName' in type ? type . displayName : 'Element'
188
+ ) ;
189
+ }
190
+ } ,
191
+ configurable : true ,
192
+ } ) ;
193
+ }
194
+ if ( ! props . hasOwnProperty ( 'ref' ) ) {
195
+ Object . defineProperty ( props , 'ref' , {
196
+ get : function ( ) {
197
+ if ( ! specialPropRefWarningShown ) {
198
+ specialPropRefWarningShown = true ;
199
+ warning (
200
+ false ,
201
+ '%s: `ref` is not a prop. Trying to access it will result ' +
202
+ 'in `undefined` being returned. If you need to access the same ' +
203
+ 'value within the child component, you should pass it as a different ' +
204
+ 'prop. (https://fb.me/react-special-props)' ,
205
+ 'displayName' in type ? type . displayName : 'Element'
206
+ ) ;
207
+ }
208
+ } ,
209
+ configurable : true ,
210
+ } ) ;
211
+ }
212
+ }
213
+ }
162
214
return ReactElement (
163
215
type ,
164
216
key ,
0 commit comments