$refs doesn't work when using v-for and destructuring
See original GitHub issueVersion
1.1.1
Reproduction link
https://github.com/adrianosmond/refs-v-for-and-rest-destructuring
Steps to reproduce
The repo above is set up so that running the tests (with npm run test
) will fail on the component (src/components/Broken.vue) which combines object rest destructuring, ref=“” and v-for like so:
<li v-for="{key, label, ...toBind} in items" :key="key" ref="listItem">
<a v-bind="toBind">{{ label }}</a>
</li>
This works fine in Vue itself, as can be seen if you run the project (npm run serve
) and open it in your browser on port 8080.
A component (src/components/Working.vue) is also included which is identical except that it doesn’t use rest destructuring inside v-for. The same test then passes for this component.
What is expected?
$refs.listItem
should be an array containing 2 <li>
elements
What is actually happening?
$refs.listItem
is undefined
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (9 by maintainers)
Top Results From Across the Web
Cannot destructure ref property as it is undefined trying to use ...
Cannot destructure ref property as it is undefined trying to use a multiple ref context. Save this question. Show activity on this post....
Read more >Reactivity Transform - Vue.js
If a destructured value is not a ref (e.g. a function), it will still work - the value will be wrapped in a...
Read more >Ref patterns, destructuring, and invisible borrows - Medium
So the lesson is that when destructuring is involved, it's not enough to look for & to find the borrows of a given...
Read more >Vue 3 Composition API: How to Omit `.value` in refs
Previously, if you tried to destructure an object that was a ref , the destructured variables would lose their reactivity. Let's go with...
Read more >10. Destructuring - Exploring JS
Don't start a statement with a curly brace ... Array destructuring (works for all iterable values): ... That means that it works with...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Nice, seems I won’t need to spend much more time on this. 3.0.7 is a really old build, glad you got it working. New Vue CLI templates ship the latest vue2-jest, so should be good moving forward.
Going to close this one off as solved/wontfix.
@lmiller1990 many thanks for the explanation! You can stop wondering: it works with
jest@27
and latest@vue/vue2-jest
. I’m sry for not checking basic setup instructions and consequently reviving this thread. Cheers!