RFC: Provide an easy way to reset the Signal Store to it's initial state #4574
Closed
abelokon0711
started this conversation in
Ideas
Replies: 1 comment 2 replies
-
You can use a custom feature for that as well: function withReset() {
return signalStoreFeature(
withState({ _resetState: {} }),
withMethods((store) => ({
resetState() {
const initialState = store._resetState();
console.log(initialState);
patchState(store, { ...initialState });
},
})),
withHooks((store) => ({
onInit() {
const { _resetState, ...state } = getState(store);
patchState(store, { _resetState: structuredClone(state) });
},
}))
);
} Full example here: Let me now, how it works. |
Beta Was this translation helpful? Give feedback.
2 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?
Signal Store
Information
In one of our stores, we significantly expand the state using custom features. Currently, to reset the state completely, we must manually list all existing state properties along with their initial values in the consuming store. This approach feels cumbersome and counterintuitive. It would be helpful to have a simple method that resets the state to its initial values.
I've tried to address this by creating a custom feature that adds an object to the state with all the initial values, which we use to patch the state for a reset. However, I am uncertain if there's a better way to access the store within a custom feature, as the current implementation is a somewhat dirty workaround.
I would be willing to submit a PR to fix this issue
Beta Was this translation helpful? Give feedback.
All reactions