Skip to content

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

Merged
merged 1 commit into from
Dec 17, 2018

Conversation

gsmnv
Copy link
Contributor

@gsmnv gsmnv commented Dec 5, 2018

List Rendering guide doesn't mention what types of values user can supply for key of v-for. There is a console warning for this already but it's hard to point people to it. This pull request fixes the issue.

@@ -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):
Copy link
Member

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.

Copy link
Contributor Author

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.

@@ -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>
Copy link
Member

@phanan phanan Dec 11, 2018

Choose a reason for hiding this comment

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

Suggested change
<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 ;)

Copy link
Contributor Author

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! 👍

@@ -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>
Copy link
Member

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 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@phanan Fixed, sorry. 🤦‍♂️

@phanan phanan merged commit 7b9f5d7 into vuejs:master Dec 17, 2018
@phanan
Copy link
Member

phanan commented Dec 17, 2018

Thanks!

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.

2 participants