Description
Vue version
3.5.12
Link to minimal reproduction
Steps to reproduce
Use a ref on at least two elements, outside of a v-for, in order to create a referenced element. The last element in the DOM will be the referenced element. Add a v-if check on another element for the ref-element.
This is the same code as in the minimal reproduction:
<script setup>
import { ref } from 'vue'
const test = ref()
</script>
<template>
<span ref="test">test 1</span>
<span ref="test">test 2</span>
<div v-if="test">
test 3
</div>
</template>
What is expected?
I was expecting for the code to work without any error messages, or give an error message that is possible to understand.
What is actually happening?
Uncaught (in promise): Maximum recursive updates exceeded in component . This means you have a reactive effect that is mutating its own dependencies and thus recursively triggering itself. Possible sources include component template, render function, updated hook or watcher source function.
System Info
No response
Any additional comments?
It is not clear why this leads to a recursion error, and that makes it hard to debug when you accidentally end up in this situation.
I think a good solution to this problem would be to throw another exception than a Recursion here. Perhaps something like "Duplicate referenced element outside of v-for"? Or perhaps it could "just work", like it does if you wrap the referenced element in a computed, like this:
<script setup>
import { computed, ref } from 'vue'
const test = ref()
const test2 = computed(() => test.value)
</script>
<template>
<span ref="test">test 1</span>
<span ref="test">test 2</span>
<div v-if="test2">
test 3
</div>
</template>