Open jsheunis opened 1 month ago
Now looking into state management, specifically: https://vuejs.org/guide/scaling-up/state-management.html#simple-state-management-with-reactivity-api
However, the simplicity starts to break down when we have multiple components that share a common state:
- Multiple views may depend on the same piece of state.
- Actions from different views may need to mutate the same piece of state.
For case one, a possible workaround is by "lifting" the shared state up to a common ancestor component, and then pass it down as props. However, this quickly gets tedious in component trees with deep hierarchies, leading to another problem known as Prop Drilling.
For case two, we often find ourselves resorting to solutions such as reaching for direct parent / child instances via template refs, or trying to mutate and synchronize multiple copies of the state via emitted events. Both of these patterns are brittle and quickly lead to unmaintainable code.
A simpler and more straightforward solution is to extract the shared state out of the components, and manage it in a global singleton. With this, our component tree becomes a big "view", and any component can access the state or trigger actions, no matter where they are in the tree!
Some of my quick interpretations:
https://vuejs.org/guide/reusability/composables.html is good reading...
Documentation here: https://vuejs.org/guide/components/provide-inject.html
Some excerpts:
Such scenarios were dealt with in exactly that way in
datalad-catalog
using Vue 2. Now Vue 3 provides a nifty alternative:This can be especially useful when considering data entry into a form that consists of multiple nested components, and where all data entered into nested components should ideally be accessible (and reactively so) to the top-level component or app instance where the collated data object (or data graph) will be maintained. Vue 3 has a suggestion for this here: