DEV Community


Discussion on: State Management with a Single Line of Code

dabalyan profile image
Ankit Singh Author

hey MadeInLagny, thanks :)

The general idea is that you access the Units directly, wherever required, however if two Units make more sense together, you can keep them separate but get their combined value by grouping them together using a Cluster.

// two Units
export const unit1 = new DictUnit({initialValue: {a: 1}});
export const unit2 = new DictUnit({initialValue: {b: 2}});
// grouped together
export const myGroup = new Cluster({unit1, unit2}) // using shorthand notation
// subscribe for reactive grouped value access
myGroup.subscribe(groupedValue => console.log(groupedValue))
// you'll see {unit1: {a: 1}, unit2: {b: 2}} in the console immediately and 
// it'll also log any future values
Enter fullscreen mode Exit fullscreen mode
inlagny profile image

But how do you access myGroup value in a component where it was not initialised ?

Thread Thread
dabalyan profile image
Ankit Singh Author

ahh I see, do you mean how to share a Unit or Cluster with multiple components?

you initialize them and export them from a separate file, then you can access them wherever needed, with a simple/direct import.