Consider providing a default key for dynamic children
See original GitHub issueBecause 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:
- Created 9 years ago
- Reactions:5
- Comments:6 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
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”
If no keys are provided, React uses the index in the array which is almost always better than a random key.