Very Basic, Ref doesn't react to property change as expected #9116
-
Hello! This is embarrassingly basic, but the struggle is getting to me, something in the fundamentals of reactivity I'm missing...
My understanding was pointing a ref to some property creates a reactive variable that tracks when the argument is changed, and then updates automatically. Is there something I'm missing in the fundamentals? I've poured over the documentation, tried watchers which ofc don't work because it's not reactive. What am I doing wrong? I feel like I ran into this before and resolved by returning the var, but that was in Pinia. Vue Essentials, Reactivity Fundamentals Thanks in advance, as I'm losing my mind! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
There are several aspects form why this won't work. Disclaimer: Before I go into them, I'll just say that the recommendation is to not use complex 3rd party objects (like i.e. With that out of the way, here's why your code does not work - it's fundamentaly a misunderstanding about Javascript foremost: You are severing the connection between the property const position = Tone.Transport.position
const transportPosition = ref(position); Question: what will happen to the The same goes for the ref you created. it will contnue to hold the initial position as its value. Now you could try and create a reactive proxy for the const position = ref()
// https://tonejs.github.io/docs/14.7.77/Transport#scheduleRepeat
Tone.Transport.scheduleRepeat((time) => {
position.value = time // or use Tone.Transport.position if you need to, I'm not familiar with that library
}, "8n") |
Beta Was this translation helpful? Give feedback.
There are several aspects form why this won't work.
Disclaimer: Before I go into them, I'll just say that the recommendation is to not use complex 3rd party objects (like i.e.
Tone.Transport
) as reactive data. complex classes like this one have to many ways in which they cn change their internal state that bypasses reactivity's change detection. Vue's recommendation is to use plain objects for reactive state only.With that out of the way, here's why your code does not work - it's fundamentaly a misunderstanding about Javascript foremost:
You are severing the connection between the property
Tone.Transport.position
and the initial value of it that you pass toref()
. Here's your code, sligh…