Why do you need to use key attribute on for directive?

Posted by Jennifer | Updated on

In order to track each node’s identity, and thus reuse and reorder existing elements, you need to provide a unique key attribute for each item with in v-for iteration. An ideal value for key would be the unique id of each item.

Let us take an example usage,

Hence, It is always recommended to provide a key with v-for whenever possible, unless the iterated DOM content is simple.

Note: You shouldn’t use non-primitive values like objects and arrays as v-for keys. Use string or numeric values instead.


If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers