Replies: 1 comment
-
Hi @gmfun, This is by design. Main reasons:
function withPatchState<State extends object>() {
return signalStoreFeature(
{ state: type<State>() },
withMethods((store) => ({
patchState(...updaters: Array<Partial<State> | PartialStateUpdater<State>>) {
patchState(store, ...updaters);
},
}))
);
}
const CounterStore = signalStore(
withState({ count: 0 }),
withPatchState(),
withMethods(({ patchState }) => ({
increment() {
patchState(({ count }) => ({ count: count + 1 }));
},
decrement() {
patchState(({ count }) => ({ count: count + 1 }));
},
}))
);
@Component({ /* ... */ })
class CounterComponent {
constructor() {
const store = inject(CounterStore);
store.increment();
store.decrement();
store.patchState({ count: 0 });
}
}
const TodosStore = signalStore(
withState({ todos: [] as Todo[] }),
withMethods((store) => ({
addTodo(todo: Todo) {
immerPatchState(store, ({ todos }) => {
todos.push(todo);
});
},
}))
); Hope this helps. Anyway, I'm going to convert this issue to discussion so other contributors can share their thoughts as well. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Which @ngrx/* package(s) are relevant/related to the feature request?
signals
Information
I understand that
ngrx/signals
is following functional apprach and thats why keptpatchState
as a separate function to update the signal value. But I feel this is a core feature and it should be part ofsignalStore
itself.Currently I am trying to migrate my exisitng implementation to use
signalStore
but I have lot ofsignal.set()
usage in template. Now I have a create a method in the component to just set a value of signal. It is getting little cumbersome.I can use
withMethods
to add methods to set value, but that is also similar.Describe any alternatives/workarounds you're currently using
If core team of ngrx/signal do not want to expose patch from
signalStore
, one way to go about this is to addwithPatchMethod
or something like that, which will usewithMethods
under the hood and addset
/patch
/update
methods to thesignalStore
without increasing the api surface ofsignalStore
I would be willing to submit a PR to fix this issue
Beta Was this translation helpful? Give feedback.
All reactions