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

React.Children.toArray changes element.key #5541

Closed
azazdeaz opened this issue Nov 24, 2015 · 5 comments
Closed

React.Children.toArray changes element.key #5541

azazdeaz opened this issue Nov 24, 2015 · 5 comments

Comments

@azazdeaz
Copy link

React.Children.toArray changing the childrens key property. ex.: "foo" => ".$foo"

In 0.14 sometimes (i dont know exactly when but event without toArray) i got even weirder keys when i parsing children. ex.: "foo" => "foo/.$foo"

I didn't found any mention of these things yet.
Is it documented somewhere?
Can i get the original key somehow?

@liesislukas
Copy link

don't you want to use one of these:
ref="val"
data-var="val"

@azazdeaz
Copy link
Author

Thanks for your answer @liesislukas but i don't looking for workarounds about this.

One more question i forgot:
Is element.key a React internal now that we shouldn't even read?

@sophiebits
Copy link
Collaborator

Yes, key is used to signal to React the identity of an object, and React.Children.toArray changes the key so that the identity semantics can be preserved. For example:

<div>
  <span key="header" />
  {items.map((item) => <span key={item.id} />)}
</div>

If any item has an id of header, that's okay – they won't be mixed up with the first element because the keys are scoped to the array that they're in. When you flatten [<span/>, items], each item gets its key prefixed to preserve this behavior.

If you want to read the key back off and have it unchanged (or to read it from within the component in props), please use another property name.

@azazdeaz
Copy link
Author

Okay, thanks @spicyj !

@tonix-tuft
Copy link

@sophiebits Could you please provide an example showing why React prefixes keys this way when using React.Children.* functions? E.g. React.Children.map

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants