Skip to content
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

Require key for conditionally rendered repeated components #2280

Conversation

felipemelendez
Copy link
Contributor

@felipemelendez felipemelendez commented Aug 28, 2023

This PR resolves #255

When Vue updates the DOM, it employs a diffing algorithm to enhance efficiency. If elements of the same type are added or removed, Vue attempts to optimize the process by recycling the previous element for the new one. This recycling strategy is an integral part of Vue's optimization efforts to minimize rendering overhead.

However, this clever recycling strategy can sometimes lead to unexpected issues, primarily because it may not properly update the components. By assigning unique keys to each repeated component within the scope of the component, Vue can quickly identify which items have changed, been added, or been removed.

This lint rule is designed to guard against such recycling issues by requiring a unique key attribute on repeated components that are part of a conditional family (v-if, v-else-if, v-else) within the same scope level. The rule not only checks for the presence of a key attribute but also ensures that repeated components within these conditional families have unique keys.

If a component is found to violate this rule, the lint tool will suggest a fix by proposing a unique key based on the component's name and count (e.g., "some-component-2").

By recognizing the unique key attribute, Vue will abandon its recycling strategy and perform a full removal and addition of the new element, thereby ensuring that the component is rendered correctly.

@felipemelendez
Copy link
Contributor Author

Hi, this PR is now ready for review. As the person who previously attempted to address this issue very well said, "Better late than never" 😀

docs/rules/index.md Outdated Show resolved Hide resolved
lib/rules/v-if-else-key.js Outdated Show resolved Hide resolved
…ird asterisk to an underscore is a weird formatting rule
@felipemelendez
Copy link
Contributor Author

Hi Flo, I noticed that your request for changes was still active, but I think we addressed everything, so that's why I pinged you for a re-review; I'm not sure what the next step should be... is there something that I need to do to address the CI errors? Thanks again for your support

@FloEdelmann FloEdelmann self-requested a review October 31, 2023 14:18
@felipemelendez
Copy link
Contributor Author

Esteemed reviewers, this PR has been sitting here for months and Flo has already reviewed it, so I have a few questions that I hope will help push this through:

  1. What are the next steps for this PR?
  2. Why does it still show that changes are requested after I have addressed all the requested changes?
  3. Why does it say that some checks were not successful? Is there something that I need to do to address those?

Thanks

@FloEdelmann
Copy link
Member

  1. I want to review the latest changes more thoroughly again. (That's why I self-requested a review.) After that, I'll either request/suggest more changes, or approve it and then assign it to the main maintainer @ota-meshi. I just haven't found the time to do so.
  2. That's a quirk of GitHub's UI that you can ignore.
  3. I actually don't know. Only the Netlify checks are failing; the GitHub Actions and CircleCI checks are passing. I remember we had this some time ago. @ota-meshi do you know how to resolve this? But anyway, @felipemelendez it's not caused by your changes (it also fails in other PRs), and you can ignore it for now.

lib/rules/v-if-else-key.js Outdated Show resolved Hide resolved
lib/rules/v-if-else-key.js Outdated Show resolved Hide resolved
lib/rules/v-if-else-key.js Outdated Show resolved Hide resolved
lib/rules/v-if-else-key.js Outdated Show resolved Hide resolved
Copy link
Member

@FloEdelmann FloEdelmann left a comment

Choose a reason for hiding this comment

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

LGTM now, thanks!

Copy link
Member

@ota-meshi ota-meshi left a comment

Choose a reason for hiding this comment

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

Sorry for the late review.

lib/rules/v-if-else-key.js Outdated Show resolved Hide resolved
Copy link
Member

@ota-meshi ota-meshi left a comment

Choose a reason for hiding this comment

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

LGTM! Thank you!

@ota-meshi ota-meshi merged commit 2f65e92 into vuejs:master Nov 30, 2023
16 checks passed
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.

Rule proposal: v-if-else-key
3 participants