global-hook-store icon indicating copy to clipboard operation
global-hook-store copied to clipboard

Dependent / Derived stores

Open lud opened this issue 4 years ago • 0 comments

Hi, Thank you for this library, it seems to do what I want in a simple and elegant way.

I would like to know what is the best way to achieve a derived store like in svelte.

Imagine I have data like this:

{
  activeProfileID: 1, 
  profiles: [
    {id: 2, name: "P1"},
    {id: 2, name: "P2"}
  ]
}

This data will be the initial state for a global ("parent") store that can show a list of profiles.

Now, most of my views need only the active profile, so I would like to export another store, the "derived", that will give access to the current active store.

  • When the activeProfileID would be changed on the global store, the derived store will reflect this change and be updated with the new active profile data.
  • Actions on the derived store, for instance setName(newName) simply call an action on the "parent" store to update the good profile according to the ID – everything is immutable here.

Is that possible?

Thank you.

lud avatar Mar 16 '21 17:03 lud