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

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. #8

Open
jmsktm opened this issue Sep 9, 2020 · 5 comments
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@jmsktm
Copy link

jmsktm commented Sep 9, 2020

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 jmsktm changed the title duplicate hashtags leads to duplicate key error in console. Duplicate hashtags leads to duplicate key error in console. Sep 9, 2020
@jmsktm jmsktm changed the title Duplicate hashtags leads to duplicate key error in console. 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. Sep 9, 2020
@cristianbote cristianbote added the bug Something isn't working label Sep 9, 2020
@cristianbote
Copy link
Owner

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 cristianbote added the good first issue Good for newcomers label Sep 9, 2020
@asharonbaltazar
Copy link

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

@cristianbote
Copy link
Owner

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
  );

@asharonbaltazar
Copy link

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

@jason-hwang
Copy link

Hello, all :)
Any updates?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants