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.

Different functional componens has the same key

See original GitHub issue

Version

2.5.17

Reproduction link

https://github.com/vedmaque/vue-functional-bug

Steps to reproduce

  1. create Component1
<template functional>
  <div class="a">
    <div class="b">first (template)</div>
    <div class="b">component</div>
  </div>
</template>
  1. create Component2
<template functional>
  <div class="x">
    <div class="y">second (template)</div>
    <div class="y">component</div>
  </div>
</template>
  1. 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. screenshot

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:open
  • Created 5 years ago
  • Reactions:5
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
inamandevcommented, Jul 24, 2019

I had the same issue and by defining a key in the template root element can resolve this issue. Example below:

First Component

<template functional>
  <div class="a" :key="'component1'">
    <div class="b">first (template)</div>
    <div class="b">component</div>
  </div>
</template>

Second Component

<template functional>
  <div class="x" :key="'component2'">
    <div class="y">second (template)</div>
    <div class="y">component</div>
  </div>
</template>

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.

1reaction
kimlisacommented, Apr 17, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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