Reactivity breaks for interdependent properties in slots
Vue version
3.2.45
Link to minimal reproduction
Steps to reproduce
If a setter manipulates multiple fields, changes are not detected properly if not all dependent getters are used within the same slot template.
- Create a writable computed property (A) where the setter also manipulates a backing field of another computed property (B).
- If property (B) is used in a slot template, changes to its backing field by writing to (A) are not reflected in the slot.
- Reactivity works properly if both (A) and (B) are used in the slot template
The linked sample demonstrates this and also shows that outside of slots everything works as expected too.
What is expected?
Computed properties should be re-evaluated properly in slot templates even when their dependencies are not used in the same slot.
What is actually happening?
No re-evaluation happens.
System Info
No response
Any additional comments?
Real-world use case:
- Setters are used to track changes to data (local to a component or even in a Pinia store)
- A computed "changed" flag shall be used to enabled/disable a save button that is not part of the main component but put into a slot in a toolbar component, or teleported to another location in the overall view
If a setter manipulates multiple fields, changes are not detected properly if not all dependent getters are used within the same slot template.
Your description is missing a further detail: "if a setter manipulates multiple nonreactive fields ..."
And that's expected. Vue observes that the setter has been called, but it can't observe that this setter applies changes to two different let
variables because that's a nonreactive change, and there's no way in Javascript to observe a change to a let variable.
Solution: turn these two let variables into ref()
's, that could be observed again.
Hi Thorsten, thanks for the fast response. I can understand this, but I'm still curious why this works when both computed properties are used side by side, and also why it works for multiple instances of the same (directly manipulated) property in different places.
Edit: I can confirm this was the issue with my much more complicated real-world app. Again I'm curious though: reactivity seemingly worked for many, slightly different experiments of mine "auto-magically" with non-ref fields, and only broke down for that specific setup?
I'm not sure what Im supposed to look for in that second example.
Generally said:
- Vue tracks property reads, and re-reruns effects (i.e.: rendering) when those properties are being set.
- Each component tracks its own reactive dependencies
- In your example, the first two child components components only track one property each: the first tracks
data.isHallo
, the second tracksdata.message
- So when you set
data.isHallo
, only the first one will re-render. The second will not, becausedata.message
has not been set. The fact that you set a let variable that holds the value for theget message
getter is unknown to Vue. So the second component will not re-render because as far as it can tell, it's reactive dependencies have not been changed. - And likewise, if you set
data.message
, only the second one will re-render. - However, the third one depends on both of those properties, so when any one of those properties' setters is being set, it will update the whole template with the latest data. So if it re-renders because you have set
isHallo
, then it will read both property's getters in the update, and thus update the dom with both properties' latest values, even though it did not actually detect a change ofdata.message
Thank you so much for taking the time to explain the details! With these I was able to observe the chain of events in the debugger, in particular your first and last bullet points. To confirm, what I was seeing was:
- When you set a (reactive) property, a render is triggered for all places reading that property, no matter what. This is the reason direct updates of properties work "reactively" even when the backing field is non-reactive, and even across multiple locations where bindings to the same property exist in a template.
- Re-renders cannot be arbitrarily granular but need to happen along certain boundaries, for example components. If setting a property causes a re-render of an element, sibling elements etc. will be also re-rendered, which effectively re-evaluates the bindings of these siblings too. This is the reason I've seen "reactivity" with non-reactive backing fields in many constellations. In reality, there was no reactivity, but direct updates of other properties with data bindings "nearby" coincidentally refreshed the non-reactive places too.
Sorry about the confusion around my second link. I just added a missing ".value" statement in your fix which broke one of the bindings (just so others who may stumble upon this issue and want to test for themselves have a fully working version).
Thanks again!