react-slick icon indicating copy to clipboard operation
react-slick copied to clipboard

Encountered two children with the same key warning

Open chrischen opened this issue 10 years ago • 23 comments

Encountered two children with the same key, .$.$1. Child keys must be unique; when two children share a key, only the first child will be used.

It keeps showing this error when mounted and also when changing slides. React 15.0.1, Chrome 50.0.2661.94.

chrischen avatar May 08 '16 03:05 chrischen

Can you give me code snippet to replicate this issue

akiran avatar May 08 '16 03:05 akiran

` render() { let settings = { dots: false, infinite: true, slidesToShow: 1, slidesToScroll: 1 };

// const childElements = this.renderResults();
// const childElements = (<div>Test1</div><div>test2</div>);

  return (
    <Slider {...settings}>
    <div key="a1"><h3>1</h3></div>
    <div key="a2"><h3>2</h3></div>
  </Slider>
  );

}`

Here is the picture of the "slick-track" div that shows 4 child components (2 with duplicate keys). screenshot 2016-05-08 12 32 01

I should also note the warning changed since I changed the name of the keys:

Encountered two children with the same key, .$.$a2. Child keys must be unique; when two children share a key, only the first child will be used.warning Encountered two children with the same key, .$.$a1. Child keys must be unique; when two children share a key, only the first child will be used.

The error does not show up and no duplicate child components are made if there is only 1 slide.

chrischen avatar May 08 '16 03:05 chrischen

I have the same issue in a project I've started today, the strange thing is I've just copied the code from another working project. The only difference I can notice between both projects is on installed dependencies inside the react-slick node_modules directory, In one the slick-carrousel version points to 1.5.9 and the other to 1.6.0, which was released last friday. According to the current package.json, the version should not be greater than 1.6.0

captura de pantalla 2016-05-08 a las 18 38 55

emartini avatar May 08 '16 21:05 emartini

i have same issue with v0.12.1 downgrading to 0.12.0 solves the problem.

package.json:

"react-slick": "0.12.0",

liesislukas avatar May 09 '16 05:05 liesislukas

I will fix it today

akiran avatar May 09 '16 06:05 akiran

Getting the same problem, It looks to me like the duplicate key warnings might be from the extra slides that get created to do the infinite looping.

corbanbrook avatar May 10 '16 00:05 corbanbrook

checkout 0.12.2 version

akiran avatar May 10 '16 10:05 akiran

I still see this issue in 0.12.2 :(

dsteinbach avatar Jul 01 '16 18:07 dsteinbach

Same here :( 2016-07-05_1616

jsiebern avatar Jul 05 '16 14:07 jsiebern

Yeah, still get the warning with [email protected].

Warning: flattenChildren(...): Encountered two children with the same key, `cloned1`. Child keys must be unique; when two children share a key, only the first child will be used.

creeperyang avatar Jul 12 '16 07:07 creeperyang

I had the same issue, but I started to deep inside the code and I got this function in track.js file

var getKey = function getKey(child, fallbackKey) {
  // key could be a zero
  return child.key === null || child.key === undefined ? fallbackKey : child.key;
};

this function define what key use when key attr is no passed, but it is, so always return child.key, so, what I did to solved the warnings for the slick and react, just add this.

var getKey = function getKey(child, fallbackKey) {
  // key could be a zero
  return child.key === null || child.key === undefined ? fallbackKey : child.key + new Date().getTime();
};

or this works as well

var getKey = function getKey(child, fallbackKey) {
  // key could be a zero
  return child.key === null || child.key === undefined ? fallbackKey : child.key + fallbackKey;
};

I hope, it is useful for you.

johuder33 avatar Jul 16 '16 03:07 johuder33

@akiran Still get the warning with [email protected].

smilingpoplar avatar Jul 22 '16 06:07 smilingpoplar

@smilingpoplar Can you replicate the issue in jsfiddle and post it hear

akiran avatar Jul 22 '16 06:07 akiran

@akiran Sorry, I don't know how to setup a react-slick example in fiddle. I've just used @johuder33 ‘s quick patch to work around this issue.

smilingpoplar avatar Jul 22 '16 06:07 smilingpoplar

why issues is closed if people are still experiencing this issue?

liesislukas avatar Jul 25 '16 05:07 liesislukas

More discussion is going on here https://github.com/akiran/react-slick/pull/362

akiran avatar Jul 25 '16 05:07 akiran

This issue is fixed in 0.12.3

akiran avatar Aug 14 '16 12:08 akiran

I am getting the same error how do i correct this. My table.js file- render() { return ( <div className="table" > <div className="container" > <div className="show-grid row"> <div className="col-xs-3 head1">Products</div> <div className="col-xs-3 head2">Variants</div> <div className="col-xs-3 head3">PSID</div> <div className="col-xs-3 head4">Updated</div> </div> {this.props.dataProp.map((data => { return (<Category key={data.categoryName + 'abc'} categoryData={data}/>); })) } </div> </div> ) }

My category.js file - render() { return ( <div className="show-grid row category-row" key={this.props.categoryData.brandCategoryId + '123'}> <div className="col-md-8 text-left category" key={this.props.categoryData.categoryName + 'abcd'} > <b>{this.props.categoryData.categoryName}</b> </div> {this.props.categoryData.productList.length > 0 && <div className="col-md-4 text-right"> <button className="edit" onClick={() => this.edit(this.props.categoryData.productList[0].brandProductSkuList)}> {this.state.text}</button> </div> } {this.renderParent.bind(this)()} </div> ) } }

ERROR---> flattenChildren(...): Encountered two children with the same key, 2:$King Combo. Child keys must be unique; when two children share a key, only the first child will be used. in div (at Category.js:83) in Category (created by Connect(Category)) in Connect(Category) (at Table.js:25)

flattenChildren(...): Encountered two children with the same key, 1:$Make it a Combo - Mutton Whopperabc. Child keys must be unique; when two children share a key, only the first child will be used. in div (at Table.js:17) in div (at Table.js:16) in DisplayTable (at map.js:90)

sugandhhgoyal avatar Jul 19 '17 05:07 sugandhhgoyal

I can confirm that I also get this issue, although only when using the lazy loading flag.

mikeselander avatar Jul 19 '17 05:07 mikeselander

This does not seem to be solved? I get this issue when using lazy loading, (same as @mikeselander)

reft avatar Sep 12 '18 20:09 reft