Skip to content

Migration guide section on key attribute changes #481

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

Merged
merged 2 commits into from
Sep 16, 2020

Conversation

AjaiKN
Copy link
Contributor

@AjaiKN AjaiKN commented Sep 16, 2020

Description of Problem

This is my attempt to fix #379 (a couple of undocumented breaking changes in Vue 3 related to the key attribute).

Proposed Solution

I think this is technically two separate breaking changes:

  • If you manually provide keys on v-if branches, then each branch must use a unique key.
  • <template v-for> key should be placed on the <template> tag (rather than on its children).

However, I think it's probably best to keep them on the same page because they're related and often appear at the same time. For example, they appear together in this relatively common situation:

<!-- Vue 2.x -->
<template v-for="item in list">
 <div v-if="item.isVisible" :key="item.id">...</div>
 <span v-else :key="item.id">...</span>
</template>

<!-- Vue 3.x -->
<template v-for="item in list" :key="item.id">
 <div v-if="item.isVisible">...</div>
 <span v-else>...</span>
</template>

Additional Information

In my opinion, keys are one of the more confusing parts of Vue. That's why I included a "Background" section near the top that explains what the key attribute is. I'm not really sure whether this section should be there or not, so I'm totally fine with removing the "Background" if people think it's unnecessary.

Copy link
Member

@NataliaTepluhina NataliaTepluhina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @Aurelius333! This is a great addition to migration guide 👍🏻

@NataliaTepluhina NataliaTepluhina merged commit 31c7d04 into vuejs:master Sep 16, 2020
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

Successfully merging this pull request may close these issues.

Migration guide section about v-if key
2 participants