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

Warnings/Types for the key attribute #2633

Open
skirtles-code opened this issue Nov 18, 2020 · 5 comments
Open

Warnings/Types for the key attribute #2633

skirtles-code opened this issue Nov 18, 2020 · 5 comments
Assignees
Labels

Comments

@skirtles-code
Copy link
Contributor

Version

3.0.2

Reproduction link

https://jsfiddle.net/skirtle/2czyf41n/

Steps to reproduce

The example illustrates a couple of things:

  1. Using objects for the key attribute works (this is good but undocumented).
  2. No warning is shown when using the same key (value 1) for multiple adjacent siblings.

What is expected?

A quick recap of Vue 2:

  1. Keys had to be primitives: string, number, boolean or symbol. Anything else triggered an immediate warning during rendering.
  2. Using the same key on siblings triggered a warning about duplicate keys.

Observations about Vue 3:

  1. From looking through the code it seems that keys are compared using ===. In some edge cases they are compared using a Map. Either way, objects can now be used as keys. I think this is great.
  2. From a TS perspective, keys are only permitted to be a string or number.
  3. There is a warning about duplicate keys but it is only hit during updates and only in very specific circumstances.

Based on my best guess about what the expected behaviour is I suggest the following changes:

  1. There should be a warning during rendering for duplicate keys. Duplicates indicate a developer error, even if they cause no real harm.
  2. The TS definitions should be updated to allow anything to be used as a key.
  3. The docs should be updated. Being able to use an object as a key is a great new feature and, if it's intentional, it's something worth shouting about. I'm happy to make the docs changes myself once I have confirmation of the officially supported behaviour.
@LongTengDao
Copy link
Contributor

LongTengDao commented Nov 19, 2020

vuejs/docs#698

And,

  1. how should NaN -0 be treated?
  2. I saw v-if/v-else is auto compiled to key:0 key:1 in the source. won't that cause error when the sibling is key=0 key=1 or other v-if/v-else also compiled to key:0 key:1?

@HcySunYang
Copy link
Member

I saw v-if/v-else is auto compiled to key:0 key:1 in the source. won't that cause error when the sibling is key=0 key=1 or other v-if/v-else also compiled to key:0 key:1?

The compiled render function will enter the optimization mode when patching, it respects the so-called block-tree algorithm, which has nothing to do with the key.

@HcySunYang
Copy link
Member

This PR provides the checking for duplicate keys

@skirtles-code
Copy link
Contributor Author

Just to clarify, adding the duplicate keys warning was just one, small part of this.

Primarily I was trying to get the types updated with a view to updating the docs.

@Maxim-Mazurok
Copy link

Hey guys, just want to add another reason why I believe there should be a warning for siblings with the same key:
Previously there was an issue when siblings were not updated when using v-if, so this got fixed for vue templates in the compiler by automatically adding keys: #1587
However, we ran into the same issue when using JSX: vitejs/vite-plugin-vue#304
I believe that by having proper checks in place for keys this issue could be avoided.

@pikax pikax self-assigned this Nov 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants