Clarify all use cases for `key` in the guide
See original GitHub issueCurrently when I search for key
on vuejs.org I’ll only get the “List Rendering” section. And this section only tells us key
is useful when rendering lists, as a hint for element reuse.
Since it’s a generic mechanism for Vue to identify nodes, the
key
also has other uses that are not specifically tied tov-for
, as we will see later in the guide.
I looked into the later part of the guide and found nothing about key
. In fact it’s mentioned earlier in “Conditional Rendering - Controlling Reusable Elements with key
”. The problem here is this section only tells us key
will affect element reuse but fail to mention this may happen to components as well. Later in the same section, in “Conditional Rendering - v-if
vs v-show
”, it says child components are destroyed and re-created during toggles, which may make this more confusing:
v-if
is “real” conditional rendering because it ensures that event listeners and child components inside the conditional block are properly destroyed and re-created during toggles.
According to my current understanding, key
is an essential hint for Vue across the whole system. Components and elements are all possible to be reused during conditional or list rendering. I think it would be better to give key
a separate section to clarify all possible situations when we need to use key
because of the mechanisms under the hood, in one place.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:5 (5 by maintainers)
Top GitHub Comments
What if we put this section highlighted and move it to the top (like above)? This may help ensure people get the general idea of
key
’s usage. And we need to rephrase a little bit becausekey
is mentioned in Conditional Rendering section which is before List Rendering. (Maybe the hint forkey
can be added in the front of Controlling Reusable Elements withkey
too.)I wouldn’t say how
key
works is an implementation detail, because there are many cases (like with<keep-alive>
) where I would only know when/why I’d usekey
if I understand how it works. I can see a page on object constancy in general being useful.