v-for loop on template with undefined keys on the child element
Vue version
3.2.37
Link to minimal reproduction
Steps to reproduce
Have a mouse. Open the SFC Playground link, hover over the "HR" category on the left so that it highlights. Then go to the search input field and type the letter "a". Observe the error message in the console:
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'el')
at patchBlockChildren (vue.runtime.esm-browser.js:6743:22)
What is expected?
I expect that the list of links on the right is filtered based on the input of the search field. So in this case I except the "Website" link to disappear and to only see the "App" links.
What is actually happening?
An error is logged and the "Website" link gets renamed to "App".
System Info
No response
Any additional comments?
While making the minimal reproduction I noticed that when I delete the v-if
div from line 76 to 82 (and remove the corresponding v-else
on the div that remains) the compiler throw an error: <template v-for> key should be placed on the <template> tag.
Applying that fix solves the problem, also on the code where the v-if
still exists.
That compiler error matches with what is written in the documentation: https://vuejs.org/guide/essentials/list.html#maintaining-state-with-key, which seems to imply that a v-if
on the child of a template confuses the compiler such that it doesn't throw the error?
Alternatively, if putting the key on the child of a template is allowed, then something goes wrong in the runtime when updating the elements if all of the keys are undefined
. I just migrated from Vue 2 to Vue 3, somewhere already before the migration the id
fields of "categories" and "applications" were renamed in the backend to pk
but I never updated the frontend. This id
field is used as the key
for some v-for
loops. The error disappears when you replace .id
with .pk
on lines 76 and 83.
Interestingly when applying the previous fix of putting the key on the template but keeping .id
instead of .pk
the application also does not throw an error.
Note that the error doesn't trigger on all actions. If you search without first highlighting the category it will not trigger. Also if you do highlight, but then type "w" the error doesn't trigger. It seems as though there needs to be an existing list of DOM elements, and the new DOM list should not contain the first element of the existing list. Or something along those lines.
You are using application.id but in your application object there is no such property, maybe you should use application.pk