-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Specify which types are allowed as values for 'key' of 'v-for' #1900
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
Conversation
src/v2/guide/list.md
Outdated
@@ -221,7 +221,7 @@ When Vue is updating a list of elements rendered with `v-for`, by default it use | |||
|
|||
This default mode is efficient, but only suitable **when your list render output does not rely on child component state or temporary DOM state (e.g. form input values)**. | |||
|
|||
To give Vue a hint so that it can track each node's identity, and thus reuse and reorder existing elements, you need to provide a unique `key` attribute for each item. An ideal value for `key` would be the unique id of each item. This special attribute is a rough equivalent to `track-by` in 1.x, but it works like an attribute, so you need to use `v-bind` to bind it to dynamic values (using shorthand here): | |||
To give Vue a hint so that it can track each node's identity, and thus reuse and reorder existing elements, you need to provide a unique `key` attribute for each item. An ideal value for `key` would be the unique id of each item, also avoid using non-primitive values as key, use string/number value instead. This special attribute is a rough equivalent to `track-by` in 1.x, but it works like an attribute, so you need to use `v-bind` to bind it to dynamic values (using shorthand here): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The added text feels pretty out of place. I'd suggest adding a p.note
at the end of the section instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@phanan Good point, but did you mean p.tip
? I've updated this PR.
src/v2/guide/list.md
Outdated
@@ -233,6 +233,8 @@ It is recommended to provide a `key` with `v-for` whenever possible, unless the | |||
|
|||
Since it's a generic mechanism for Vue to identify nodes, the `key` also has other uses that are not specifically tied to `v-for`, as we will see later in the guide. | |||
|
|||
<p class="tip">Avoid using non-primitive values as key, use string/number value instead.</p> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<p class="tip">Avoid using non-primitive values as key, use string/number value instead.</p> | |
<p class="tip">Don't use non-primitive values like objects and arrays as `v-for` keys. Use string or numeric values instead.</p> |
I know the message is taken directly from Vue's warning, but for documentation purpose, it doesn't need to be that brief ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@phanan Fixed, thanks for suggestion! 👍
src/v2/guide/list.md
Outdated
@@ -233,6 +233,8 @@ It is recommended to provide a `key` with `v-for` whenever possible, unless the | |||
|
|||
Since it's a generic mechanism for Vue to identify nodes, the `key` also has other uses that are not specifically tied to `v-for`, as we will see later in the guide. | |||
|
|||
<p class="tip">Don't use non-primitive values like objects and arrays as `v-for` keys. Use string or numeric values instead</p> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You miss a punctuation here :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@phanan Fixed, sorry. 🤦♂️
Thanks! |
List Rendering guide doesn't mention what types of values user can supply for
key
ofv-for
. There is a console warning for this already but it's hard to point people to it. This pull request fixes the issue.