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

Encountered two children with the same key, `#hashtag`. Keys should be unique so that components maintain their identity across updates. Non-unique keys may cause children to be duplicated and/or omitted — the behavior is unsupported and could change in a future version.

Open jmsktm opened this issue 4 years ago • 5 comments

Error message: Encountered two children with the same key, #hashtag. Keys should be unique so that components maintain their identity across updates. Non-unique keys may cause children to be duplicated and/or omitted — the behavior is unsupported and could change in a future version.

Note #hashtag #hashtag in index.js. image

jmsktm avatar Sep 09 '20 02:09 jmsktm

Hey @jmsktm, thanks for opening this issue. Fantastic reproduction! Should be straight forward to fix it. Do you wanna tackle it? If not I can surely do it.

cristianbote avatar Sep 09 '20 05:09 cristianbote

I'll take it! Can I ask how the keys are passed down?

asharonbaltazar avatar Sep 09 '20 15:09 asharonbaltazar

Awesome @asharonbaltazar! Thank you!

Here it is in the default hashtag renderer https://github.com/cristianbote/react-hashtag/blob/master/src/index.js#L24. I think the most straightforward way to achieve uniqueness is to keep a counter outside and compute the key based on it.

let counter = 0;

const defaultHashtagRenderer = createElement => (hashtag, onClick) =>
  createElement(
    "span",
    {
      key: hashtag + counter++,
      onClick: onClick ? e => onClick(hashtag, e) : null
    },
    hashtag
  );

cristianbote avatar Sep 10 '20 06:09 cristianbote

Love the enthusiasm! How would you go about debugging a React component? First time doing this, so I'd appreciate any help.

asharonbaltazar avatar Sep 10 '20 14:09 asharonbaltazar

Hello, all :) Any updates?

jason-hwang avatar Jul 21 '21 07:07 jason-hwang