Support but warn on key={null}
Fixes #2386. Test Plan: jest
Ben Alpert committed
Oct 20, 2014 at 17:03 UTC
dd92786fb038c4eb7c6545928646ec8aa9ecfa36
2 files changed
+26
-1
src/core/ReactElement.js
+10
-1
@@ -137,7 +137,16 @@ ReactElement.createElement = function(type, config, children) {
137
138
if (config != null) {
139
ref = config.ref === undefined ? null : config.ref;
140
- key = config.key === undefined ? null : '' + config.key;
140
+ if (__DEV__) {
141
+ warning(
142
+ config.key !== null,
143
+ 'createElement(...): Encountered component with a `key` of null. In ' +
144
+ 'a future version, this will be treated as equivalent to the string ' +
145
+ '\'null\'; instead, provide an explicit key or use undefined.'
146
+ );
147
+ }
148
+ // TODO: Change this back to `config.key === undefined`
149
+ key = config.key == null ? null : '' + config.key;
150
// Remaining properties are added to a new props object
151
for (propName in config) {
152
if (config.hasOwnProperty(propName) &&
src/core/__tests__/ReactElement-test.js
+16
@@ -81,6 +81,22 @@ describe('ReactElement', function() {
81
expect(element.props).toEqual({foo:'56'});
82
});
83
84
+ it('treats a null key as omitted but warns', function() {
85
+ spyOn(console, 'warn');
86
+ var element = React.createFactory(ComponentFactory)({
87
+ key: null,
88
+ foo: '56'
89
+ });
90
+ expect(element.type).toBe(ComponentClass);
91
+ expect(element.key).toBe(null); // as opposed to string 'null'
92
+ expect(element.ref).toBe(null);
93
+ expect(element.props).toEqual({foo:'56'});
94
+ expect(console.warn.argsForCall.length).toBe(1);
95
+ expect(console.warn.argsForCall[0][0]).toContain(
96
+ 'will be treated as equivalent to the string \'null\''
97
+ );
98
+ });
99
+
100
it('preserves the context on the element', function() {
101
var Component = React.createFactory(ComponentFactory);
102
var element;