Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore children with clashing keys #1364

Merged
merged 1 commit into from
Apr 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/utils/ReactChildren.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

var PooledClass = require('PooledClass');

var invariant = require('invariant');
var traverseAllChildren = require('traverseAllChildren');
var warning = require('warning');

var twoArgumentPooler = PooledClass.twoArgumentPooler;
var threeArgumentPooler = PooledClass.threeArgumentPooler;
Expand Down Expand Up @@ -86,16 +86,21 @@ PooledClass.addPoolingTo(MapBookKeeping, threeArgumentPooler);
function mapSingleChildIntoContext(traverseContext, child, name, i) {
var mapBookKeeping = traverseContext;
var mapResult = mapBookKeeping.mapResult;
var mappedChild =
mapBookKeeping.mapFunction.call(mapBookKeeping.mapContext, child, i);
// We found a component instance
invariant(
!mapResult.hasOwnProperty(name),

var keyUnique = !mapResult.hasOwnProperty(name);
warning(
keyUnique,
'ReactChildren.map(...): Encountered two children with the same key, ' +
'`%s`. Children keys must be unique.',
'`%s`. Child keys must be unique; when two children share a key, only ' +
'the first child will be used.',
name
);
mapResult[name] = mappedChild;

if (keyUnique) {
var mappedChild =
mapBookKeeping.mapFunction.call(mapBookKeeping.mapContext, child, i);
mapResult[name] = mappedChild;
}
}

/**
Expand Down
14 changes: 8 additions & 6 deletions src/utils/__tests__/ReactChildren-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,17 +320,19 @@ describe('ReactChildren', function() {
}).not.toThrow();
});

it('should throw if key provided is a dupe with explicit key', function() {
it('should warn if key provided is a dupe with explicit key', function() {
var zero = <div key="something"/>;
var one = <div key="something" />;
var one = <span key="something" />;

var mapFn = function() {return null;};
var mapFn = function(component) { return component; };
var instance = (
<div>{zero}{one}</div>
);

expect(function() {
ReactChildren.map(instance.props.children, mapFn);
}).toThrow();
spyOn(console, 'warn');
var mapped = ReactChildren.map(instance.props.children, mapFn);

expect(console.warn.calls.length).toEqual(1);
expect(mapped).toEqual({'.$something': zero});
});
});
14 changes: 8 additions & 6 deletions src/utils/flattenChildren.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

"use strict";

var invariant = require('invariant');
var traverseAllChildren = require('traverseAllChildren');
var warning = require('warning');

/**
* @param {function} traverseContext Context passed through traversal.
Expand All @@ -29,13 +29,15 @@ var traverseAllChildren = require('traverseAllChildren');
function flattenSingleChildIntoContext(traverseContext, child, name) {
// We found a component instance.
var result = traverseContext;
invariant(
!result.hasOwnProperty(name),
'flattenChildren(...): Encountered two children with the same key, `%s`. ' +
'Children keys must be unique.',
var keyUnique = !result.hasOwnProperty(name);
warning(
keyUnique,
'flattenChildren(...): Encountered two children with the same key, ' +
'`%s`. Child keys must be unique; when two children share a key, only ' +
'the first child will be used.',
name
);
if (child != null) {
if (keyUnique && child != null) {
result[name] = child;
}
}
Expand Down