@ngrx/signals: Can a ngrx signal feature store access the state of it's consuming store? #4384
-
Can a ngrx signal feature store access the state of it's consuming store? I would like to define a feature store that takes as input(or references) a state from the store which uses/consumes the store, e.g. the parent of the ngrx signal feature store. For example, I have a store called "withCounters()" that holds a "state variable" called "counterA" of type "number". I would like to add a feature store that can access these state variables.
The "withDecrement" feature should be able to access its consumers state, e.g. the "counterA" variable.
Is this doable? If yes, how? My main question is how I can access the consumer's store state? I created a stackoverflow question here: https://stackoverflow.com/questions/78603106/can-i-pass-access-a-state-of-a-parent-store-to-a-ngrx-signal-feature-store |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
A direct access seems to be not possible without coupling the stores. Instead, the ngrx team suggests to use a wrapper function: #4340 Read the full thread here: A working example with a wrapper function: https://stackblitz.com/edit/ngrx-signal-store-starter-z3q5i5?file=src%2Fmain.ts A non working example due to coupling:https://stackblitz.com/edit/ngrx-signal-store-starter-23tsna?file=src%2Fmain.ts |
Beta Was this translation helpful? Give feedback.
-
@Donnerstagnacht. Thanks for the flowers, but I am a mere mortal and not part of the NgRx team :) |
Beta Was this translation helpful? Give feedback.
-
If Whenever you have a circular dependency in software development (store depends on withIncrement, withIncrement depends on store), you resolve it by decoupling both and introducing a third element that has a dependency on these two. In our case, that would be withFeatureFactory((store) =>
withIncrement<Counter>('items', (fn: (state: Counter) => Counter) => {
patchState(store, (value) => fn(value));
})
) Do you understand what I mean? |
Beta Was this translation helpful? Give feedback.
A direct access seems to be not possible without coupling the stores.
Instead, the ngrx team suggests to use a wrapper function: #4340
Read the full thread here:
A working example with a wrapper function: https://stackblitz.com/edit/ngrx-signal-store-starter-z3q5i5?file=src%2Fmain.ts
A non working example due to coupling:https://stackblitz.com/edit/ngrx-signal-store-starter-23tsna?file=src%2Fmain.ts