Vue For Key

Track tasks and feature requests

Dec 26, 2017 - Having a unique key solves this problem. The key acts as a sort of flag that tells Vue 'if the data associated with this child component is moved. Sep 8, 2018 - When you need the component to be re-rendered, you just change the value of the key and Vue will re-render the component. It's a pretty.

Join 36 million developers who use GitHub issues to help identify, assign, and keep track of the features and bug fixes your projects need.

Sign up for free See pricing for teams and enterprises 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

Comments

commented Jul 27, 2017
edited

What problem does this feature solve?

In our business system, there is so many :key object used.
We did so many data operate, we have to use it.
And now, object key is not advocated, and I'm wondering if there is some prop can do it automatically.
It's will be helpful.

What does the proposed API look like?

commented Jul 27, 2017
edited

You can use the index for this (this is if you don't care about Vue reusing the elements or components). Keep in mind key is not always required

more at https://vuejs.org/v2/guide/list.html#key

commented Jul 28, 2017
edited

@posva , you can see this issue : #5410

@yyx990803 :

You are using the index as the key.. which is the same as no key at all. You should give each of your data objects a unique id so that they can be keyed properly.

@posva ,I know, if there a show list, we don't need it, but we has so many edit things.

commented Jul 28, 2017

@vvpvvp, that's the point, we vue can not know how developer wants key to work, index is unique key example but most of the time it's not what you are looking for, how can Vue know what you wanted?

commented Jul 28, 2017

@nickmessing , I just want v-for has no question when i use it normal.
for example, I wrote an common table component , and it used v-for list data, and have some operate staffs, we don't know data's key prop, or there is no key completely.
how can I do, I used object as key.
but now, it's not advocated.

and, If I don't use key, there is some problem we not expected.
you can see this issus: #5408

commented Jul 28, 2017

I thought you were forcing yourself to use a key. You don't always need keys
You need unique keys for your objects, you probably have some kind of id. as @nickmessing said, Vue cannot guess it for you.. Its guess is using the index

commented Jul 28, 2017

@posva , I thought you are missing this point :

You are using the index as the key.. which is the same as no key at all. --- from yyx990803.

I don't want force myself to use a key, it's because if I don't use it, it will be something wrong.
link: https://codepen.io/vvpvvp/pen/oZKpgE
steps:
1: click 'add' twice
2: click the last line 'click me'.
3: click the first line 'delete'.

commented Jul 28, 2017
edited

that's why I told you to use the index.. because it should be the same..
You can generate a unique id with every object, and in your case, you need it. That's what we're trying to tell you

commented Jul 28, 2017
edited

Hi @vvpvvp
as @posva said you should generate a key
workaround by https://github.com/puleos/object-hash
eg: https://codepen.io/anon/pen/NvGwyQ

commented Jul 28, 2017

@vvpvvp , the point is Vue cannot infer a unique key for you, what vue can do and already does is to infer a key based on index.

If there are operations like 'add' or 'delete' elements in list, you really should generate a unique yourself. A simply counter could do the trick.

commented Jul 28, 2017
edited

@Kingwl , I understand.
so, I have a question, what's the difference between we generate and vue generate.
why not vue do it by itself, yes, you don't know what we want, so I told you, I has no key in my data when I use v-for, and I need edit some data or do some thing else, and could you please generate unique keys for us?

changed the titlev-for : add a prop 'uniqueKey' to generates a uniqueKey automaticallyJul 28, 2017

commented Jul 28, 2017
edited

@vvpvvp calm down my friend 😅
generate unique keys is so complex to cover all case
and it not too hard to generate a unique key by user

and IMO this behavior is not normal..i(we) will try to improve it
have @yyx990803 got some advice?

commented Jul 28, 2017

Vue cannot generate the unique keys for you because it doesn't know how or when you add or delete items. But it's super easy to add an id. Without using hash you can use an incrementing id: https://codepen.io/posva/pen/BdoJJz

commented Jul 28, 2017
edited

Key

he told you that is a unexpected behavior not expected behavior🌚
and add a key can resolve that

commented Jul 28, 2017
edited

IMO the feature of supporting object type keys is more constructive than this, and it's likely to be implemented in the future. See #5804 (comment)

As you know, the unique key must be attached to the item object, when it's automatically generated inside Vue, it may cause some problems if users are unaware of that.
If you really feel troublesome to generate unique ids for objects by yourself, you can write a common util function to achieve it. It's similar to the workaround @Kingwl provided, but much lighter (and may lead to a performance issue😂, you can test it in your App):

Here's a demo - https://jsfiddle.net/xujiongbo/3wt7hv7c/

commented Jul 28, 2017

By default, Vue uses the index of the items since it's the only thing it can do without having to 'guess' what the developer wants.
Vue won't generate unique keys for you, there is no point in this. The developer needs to provide unique keys because Vue can't guess them. Also, Vue mutating your objects on its own would be very wrong in my opinion.
If your data don't have any field that could be used as unique keys, there is something not right about it, ask your backend team.

commented Jul 28, 2017

@posva ,

But it's super easy to add an id.

no.....

1st, it' s ugly.

2nd, it's easy to add an id in a simple demos. but, it's not easy to add an id in our complex datas with complex system. and the data is nested structure. and it's nested and nested and nested..

finally, it 's good in vue1.0 , and we don't need key at all, I know vue mechanism changed, I just wonder if there is some way can handle it, it will be so helpful.

tips: it's not only v-for problems, some adjacent element with directive used, if you don't add some key, it's also has some problem.

@Kingwl , my English is not good, maybe my words is not readability, I'm sorry about it, but I am also peaceful.

commented Jul 28, 2017
edited

1st, it' s ugly.

Why?

2nd, it's easy to add an id in a simple demos. but, it's not easy to add an id in our complex datas with complex system. and the data is nested structure. and it's nested and nested and nested..

Don't you have fields in your data to make them unique? How do you make the relations between your objects?

finally, it 's good in vue1.0 , and we don't need key at all

If you want the default behavior, you can just use the index:

Note that you don't need this if you are looping over HTML standard elements.

commented Jul 28, 2017

I wrote very slowly, so when i complete my answer, always a new answer to me.

@javoski , you are so kindly, function genUniqueKey , I can wrote it myself, there is no problem at all.

vue can't change the datas we define, and also I can't change the data used on my ui components, like table component and so on.

I wrote an ui toolkit with vue2.0, http://www.heyui.top/component, if i used kind of genUniqueKey function, maybe i can do, but it's too hard, and i really don't like the way.

commented Jul 28, 2017

@Akryum , I don't want to answer it so many times. index is not useful, please read this demo .

link: https://codepen.io/vvpvvp/pen/oZKpgE
steps:
1: click 'add' twice
2: click the last line 'click me'.
3: click the first line 'delete'.

commented Jul 28, 2017

@vvpvvp I was answering just a part of your comment. As you should know by now, your issue will be resolved when you will provide a unique key for each item in your data.

commented Jul 28, 2017

Let's move the discussion to the Discord server (or the forum). Other people will be able to help you with insights that are more specific to your project @vvpvvp🙂
Thanks

commented Sep 7, 2017

@vvpvvp I came to a similar situation using Vue components inside a list that it can be edited (add/remove items and change the order using VueDraggable). Items haven't any unique id because they are generated data within a form. The solution I've found is to use a 'shadow' list where I save unique ids, for rendering purpose, and to edit both lists on user interaction.

Here is an example using your code
https://codepen.io/anon/pen/RZzbmM

commented Sep 8, 2017

@nauzethc, thank you very much.
I must explain that I know all this solution about solve this question.
But It's will be better if we don't need a key In the ordinary way.
It's not a problem when we use v-for, I met so many inexplicable problems in my development.

commented Sep 8, 2017

@vvpvvp, i understand your problem.
The main problem with key here is if your data realy have unique ids but, when you create new items in list/table you add them with blank ids like 000-000, then when it goes to server it generates proper uids.
But as far as your are in edit mode and adding new items you don't realy want to generate ids for them, server needs em as blank, so to handle this problem you have to add another one ids and that's not realy something you want.
Adding an option that would generate keys for inner Vue use seems legit request.

commented Mar 20, 2018

I came across this issue and resolved it by adding 'key' attribute with Date.now() value as a key.

commented Mar 21, 2018

e..
good solution?
It's mean you give up use “in-place patch” strategy

commented Jul 5, 2018

@mohd-isa
It's unsafe method. Iteration loop may take place more than one in the same Date.now() period.
Look: http://snpy.in/pjuT8K
Date.now() used in screenshot.

• On Windows 8.1 and Windows Server 2012 R2, it’s listed as Update for Microsoft Windows (KB3102467) under the Installed Updates item in Control Panel. Download microsoft net framework 4.6 1 offline installer. After you install this package, you can see that the following packages or updates were installed, depending on your operating system: • On Windows 7 SP1 and Windows Server 2008 R2 SP1, the Microsoft.NET Framework 4.6.1 is listed as an installed product under the Programs and Features item in Control Panel. We recommend that you use the web installer instead of the offline installer when possible for optimal efficiency and bandwidth requirements. • On Windows 8 and Windows Server 2012, it’s listed as Update for Microsoft Windows (KB3102439) under the Installed Updates item in Control Panel.

Actually, the problem appears in cases we can't get UID from item; items could change (they come as a props for ex.); they should take place simultaneously (transition-group, for ex.).
Thats the point – we can't set simply index, or my-awesome-item${index}, etc.

IMO, the best way is provide build-in mechanism for generating UIDs. That isn't hard to write helpers, it's hard to do it every time in every project.
Just include generator to the Vue core or add helpers (we just have Map-helpers in Vuex, why not?).

commented Jul 5, 2018

Please refer to https://vuejs.org/v2/guide/list.html#key to see all the implications about setting a key and why Vue needs it for component.
Using Date.now() for keys is a very bad idea as it will tell Vue to never reuse components.
Most of the time using the index of the iteration is fine but if your component have a state and the list may change (new elements, removed, reordered), make sure to use an id that uniquely represents the object that is passed to your component. For example: if you have a contact component that displays a contact information and has a contact prop, use the id of the contact (usually coming from the db) as the key:

A built-in mechanism to generate UID have no place in Vue as it is not related to building interfaces.
Sometimes you can use other content that is unique like names. A cleaner way if to generate a new version of your list that augment your items with UIDs

locked and limited conversation to collaborators Jul 5, 2018

Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

NCEES Computer-Based Exams NCEES is a national nonprofit organization responsible for developing and scoring the examinations used for engineering and surveying licensure in the United States, the District of Columbia, Guam, the Northern Mariana Islands, Puerto Rico, and the U.S. Virgin Islands. It has contracted with Pearson to administer and score the Fundamentals of Engineering (FE), Fundamentals of Surveying (FS), and Principles and Practice of Surveying (PS) exams. Exam Registration and Scheduling Information. Registration Registration for computer-based exams is accessible 7 days per week, 24 hours per day, through a valid MyNCEES account only.

Visit ncees.org to log in or create an account. Examinees will receive an email notification once they have successfully completed the registration process and have been authorized to schedule an exam. Scheduling Upon receiving authorization to schedule an appointment, examinees should log in to their, select the “SCHEDULE” option and follow the on-screen instructions. Appointments for computer-based exams are currently available during the following testing windows: January - March April - June July - September October - December Rescheduling and Cancellations Examinees are able to reschedule or cancel an appointment by logging into their, selecting the “Modify Appointment” option and following the on-screen instructions. All associated fees are payable to Pearson and subject to applicable fees. Please note that canceling your exam will cancel your authorization to test and require you to reapply and pay for the exam. If applicable, refunds will be automatically applied by NCEES.

Practice Exam Information NCEES discontinued computer-based practice exams on February 28, 2017. If you still have a valid computer-based practice exam, it is available through your MyNCEES account. Familiarize yourself with the look, feel and navigation of a Pearson VUE computer-based test.