Skip to content

Commit 631b6a0

Browse files
committed
Merge pull request facebook#2388 from spicyj/null-key
Support but warn on key={null}
2 parents ac3e9b5 + dd92786 commit 631b6a0

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/core/ReactElement.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,16 @@ ReactElement.createElement = function(type, config, children) {
137137

138138
if (config != null) {
139139
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;
141150
// Remaining properties are added to a new props object
142151
for (propName in config) {
143152
if (config.hasOwnProperty(propName) &&

src/core/__tests__/ReactElement-test.js

+16
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ describe('ReactElement', function() {
8181
expect(element.props).toEqual({foo:'56'});
8282
});
8383

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+
84100
it('preserves the context on the element', function() {
85101
var Component = React.createFactory(ComponentFactory);
86102
var element;

0 commit comments

Comments
 (0)