Skip to content

Commit 72643f8

Browse files
committed
Fix Invariant Violation when a single element child is contained in an array.
1 parent 13078eb commit 72643f8

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

src/linkClass.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import makeConfiguration from './makeConfiguration';
3+
import _ from './utils';
34

45
let linkClass;
56

@@ -53,14 +54,16 @@ linkClass = (element, styles = {}, userConfiguration) => {
5354
if (typeof element.props.children !== 'string') {
5455
childrenCount = React.Children.count(element.props.children);
5556

56-
if (childrenCount > 1) {
57+
if (childrenCount > 1 || _.isArray(element.props.children)) {
5758
newChildren = React.Children.map(element.props.children, (node) => {
5859
if (React.isValidElement(node)) {
5960
return linkClass(node, styles, configuration);
6061
} else {
6162
return node;
6263
}
6364
});
65+
// https://github.com/facebook/react/issues/4723#issuecomment-135555277
66+
newChildren = _.values(newChildren);
6467
} else if (childrenCount === 1) {
6568
newChildren = linkClass(React.Children.only(element.props.children), styles, configuration);
6669
}

src/utils.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import forEach from 'lodash/collection/forEach';
2+
import values from 'lodash/object/values';
3+
import isArray from 'lodash/lang/isArray';
24

35
export default {
4-
forEach
6+
forEach,
7+
values,
8+
isArray
59
};

test/linkClass.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,41 @@ describe('linkClass', () => {
2525
expect(linkClass(<div className='foo'></div>)).to.deep.equal(<div className='foo'></div>);
2626
});
2727

28-
// Using array instead of object causes the following error:
29-
// Warning: Each child in an array or iterator should have a unique "key" prop.
30-
// Check the render method of _class. See https://fb.me/react-warning-keys for more information.
31-
xit('does not affect element with multiple children', () => {
32-
expect(linkClass(<div><p></p><p></p></div>)).to.deep.equal(<div><p></p><p></p></div>);
28+
// Does not affect
29+
it('does not affect element with a single children when that children is contained in an array', () => {
30+
let outcome,
31+
subject;
32+
33+
subject = React.createElement('div', null, [
34+
React.createElement('p')
35+
]);
36+
outcome = React.createElement('div', null, [
37+
React.createElement('p')
38+
]);
39+
40+
expect(linkClass(subject)).to.deep.equal(outcome);
41+
});
42+
43+
it('does not affect element with multiple children', () => {
44+
// Using array instead of object causes the following error:
45+
// Warning: Each child in an array or iterator should have a unique "key" prop.
46+
// Check the render method of _class. See https://fb.me/react-warning-keys for more information.
47+
// @see https://github.com/facebook/react/issues/4723#issuecomment-135555277
48+
// expect(linkClass(<div><p></p><p></p></div>)).to.deep.equal(<div><p></p><p></p></div>);
49+
50+
let outcome,
51+
subject;
52+
53+
subject = React.createElement('div', null, [
54+
React.createElement('p'),
55+
React.createElement('p')
56+
]);
57+
outcome = React.createElement('div', null, [
58+
React.createElement('p'),
59+
React.createElement('p')
60+
]);
61+
62+
expect(linkClass(subject)).to.deep.equal(outcome);
3363
});
3464
});
3565

0 commit comments

Comments
 (0)