$refs not reset when destroying element on event within watch
callback on prop value change #12639
Description
Vue version
5a6e98c, 3.5.13
Link to minimal reproduction
Steps to reproduce
- Open Web Developer Tools > Console.
- Click on "printRefs" button. This prints current value of
this.$refs.other
': the instance of theother-component
. - Click on "ToggleDisplay" and then on "printRefs" again. The
other-component
disappears andthis.$refs.other
is set tonull
. - Click on "ToggleDisplay" and "printRefs" again. State equivalent to 2.
- Click on "ToggleOpen", then on "printRefs". We expect
null
, butthis.$refs.other
contains theother-component
instance!
What is expected?
Clicking on "ToggleOpen" sets the open
reactive data to false
. This reactive data is bound to the open
prop of the other-component
, who watches changes on that prop and emits an event closed
when false
. The main component listens on the @closed
event and sets the display
reactive data to false
when received. The display
reactive data is bound to the v-if
directive.
I would expect that setting display
to false
directly and indirectly (in the complicated sequence above) would lead to the same result: the component is unmounted, the instance destroyed and any $refs
cleared.
What is actually happening?
$refs
is not cleared and the instance is not destroyed.
System Info
No response
Any additional comments?
The above is a reduced example. As far as I could test the combination of prop -> watch -> emit -> v-if together with ref
is needed to reproduce the issue.
In the context where the issue originally appeared there is not a linear dependency between open
and display
, and the watch callback does not know whether any listener of the @closed
event will try to destroy the instance. Additionally, the original code uses v-for
instead of v-if
; in that case the $refs
array seems to only grow and never shrink.
Please let me know whether something is unclear or more information is needed.
Thank you.