Dealing with reactive arrays #58
ThaNarie
started this conversation in
Design challanges
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Reactivity in Vue has two concepts:
Depending on how we store arrays in our reactive data, we might need to update how we react to these changes.
reactive data
This is where we store and modify the data, and is split up in two tools:
ref
– as a container around primitivesreactive
- as a Proxy around objectsThere is also
computed
, which is a combination of the "reaction to changes" and "reactive data" itself. But don't play a big role in this issue.reacting to changes
This can generally be done in two ways:
watchEffect
simple tool, it's executing the callback once, and if anything changes, it does it again. No options to specify.watch
is more powerful. It can do the same aswatchEffect
, but also a lot more.Note; currently most of Muban's internal code relies on
watchEffect
.Issue - Objects vs Arrays
Since arrays are sort of objects, you expect them to work the same within a
reactive
proxy. Whenever you set an object key, it's detected as a change.However, with arrays you can also use methods such as
push
andpop
to mutate the array. By default, these changes are not detected inwatch
orwatchEffect
.watch
has a{ deep: true }
option that can be used. It's not super clear, but if you know it, you can change it in your own component code.But supporting this in Muban internals, would require us to change everything (where we could reasonably expect an array to be passed – or used in a computed in the middle?) to
watch
+deep
.Alternative
Instead of
reactive
arrays, we could also advocate to using aref
, and never mutate the array, but always re-assignref.value
.Caveat; I don't have any current experience with nested objects in arrays, and how to best deal with those. Although Vue does support nesting ref and reactive, so that might be fine.
Beta Was this translation helpful? Give feedback.
All reactions