question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Consider providing a default key for dynamic children

See original GitHub issue

Because many times performance is not an issue, I find myself doing things like this:

React.DOM.ul(null, items.map(function(item) {
  return React.DOM.li({key: Math.random()}, item.property)
})

Would make sense to have something like this by default?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:5
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

67reactions
zpaocommented, Apr 1, 2014

I understand where you’re coming from, but please don’t ever do what you have there. We made this mistake in one of our examples for a while and it’s just really bad. By doing this, you’ll actually end up recreating all of your nodes on every render because the key will be different. While it might not hurt you today, it will one day.

A couple other points:

  • key is not really about performance, it’s more about identity (which in turn leads to better performance). randomly assigned and changing values are not identity
  • We can’t realistically provide keys without knowing how your data is modeled. I would suggest maybe using some sort of hashing function if you don’t have ids
  • We already have internal keys when we use arrays, but they are the index in the array. When you insert a new element, those keys are wrong.

Now that said, they key warning can be pretty annoying. If it’s driving people to end up with solutions like you have here, then we should probably spend some time thinking of a better way to do this.

tl;dr: “no”

29reactions
sophiebitscommented, Apr 1, 2014

I agree a random key is the worst key one can use, but it’s still better than no key at all.

If no keys are provided, React uses the index in the array which is almost always better than a random key.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why doesn't ReactJS autogenerate keys for dynamic children?
You need to assign a unique key to dynamic children because this is how React's virtual DOM associates that element with a specific...
Read more >
About Dynamic Members
The key is proper design to fit your requirements, setting the appropriate access rights to dynamic children with the Access Granted to Member...
Read more >
Vue.js — Using provide-inject to communicate between parent ...
js — Using provide-inject to communicate between parent and dynamic child components. The <google-map> is the parent component, which initializes and renders ...
Read more >
Slots - Vue.js
Slot Content and Outlet #. We have learned that components can accept props, which can be JavaScript values of any type. But how...
Read more >
React Top-Level API
React also provides a component for rendering multiple elements without a wrapper. ... New children will replace existing children. key and ref from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found