Different functional componens has the same key
See original GitHub issueVersion
2.5.17
Reproduction link
https://github.com/vedmaque/vue-functional-bug
Steps to reproduce
- create
Component1
<template functional>
<div class="a">
<div class="b">first (template)</div>
<div class="b">component</div>
</div>
</template>
- create
Component2
<template functional>
<div class="x">
<div class="y">second (template)</div>
<div class="y">component</div>
</div>
</template>
- render them in App.
<template>
<div id="app">
<component1 />
<component2 />
</div>
</template>
What is expected?
Everything works fine
What is actually happening?
Vue warns about same key
[Vue warn]: Duplicate keys detected: '__static__0'. This may cause an update error.
If you create the same components from render function directly, it works fine, without duplicated keys (keys are undefined
in this situation)
<script>
export default {
functional: true,
render(createElement) {
return createElement("div", {
staticClass: "a"
}, [createElement("div", {
staticClass: "b"
}, ["first"]), createElement("div", {
staticClass: "b"
}, ["component"])])
}
}
</script>
This image shows the difference in VNode objects.
Moreover, if Component1
looks likes this, it works fine too.
<template functional>
<div class="a">first (template) component</div>
</template>
Vue Template Exporer will go crazy too if you try to compile Component1
https://template-explorer.vuejs.org/#<div class%3D"a">
<div class%3D"b">first (template)<%2Fdiv>
<div class%3D"b">component<%2Fdiv>
<%2Fdiv>
Issue Analytics
- State:
- Created 5 years ago
- Reactions:5
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Encountered Two Children with the Same Key — React Fix
A key prop should be unique, stable and reproducible. Unique: The key for an element should be unique amongst its siblings. The key...
Read more >Functional Stateless Components and Keys - Stack Overflow
I'm using Map in one functional component to create a list of another element, but when I use 'key' to remove the console...
Read more >Understanding Functional Components vs. Class ... - Twilio
A class component is a JavaScript class that extends React. Component which has a render method.
Read more >Types of React Components. Functional, Class, Pure, and…
React components are independent and reusable code. They are the building blocks of any React application. Components serve the same purpose as JavaScript ......
Read more >React Class Component vs Functional Component - Telerik
From the demonstration, the apparent difference is the syntax. Personally, I found the functional component easier to understand compared to the ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop 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
Top GitHub Comments
I had the same issue and by defining a key in the template root element can resolve this issue. Example below:
First Component
Second Component
After defining the key to the very root element of the template you can solve this issue. You can see the static generated keys in Vue-Dev Tools(Browser) as well, so defining your own key attribute resolve the issue. Once you add your key attribute in template’s root element, you can see your defined keys in Vue-Dev Tools.
Please let me know your outcome on this.
I had the same warning using version
2.6.10
. I had a component using three child functional components and it would give me this warning, so what I did was assign the root element for each functional component with a unique key from each other and the warning went away. Not sure if that was right though but it got rid of the error.