This plugin is designed to clear, reset, and overwrite NGXS states on dispatch of predefined actions. Please check this playground for a working example.
Please use the table below to make sure you are using the correct version of this library.
Angular version |
ngxs-reset-plugin version |
---|---|
18+ | 4 |
14+ | 3 |
13 | 2 |
12 | 2 |
< 12 | 1 |
If you wonder why you would need a plugin like this, when there is already a reset method in NGXS, you may find the reasons below. Otherwise, skip to the installation or usage section.
Calling the reset method of NGXS Store like store.reset(myNewStateObject)
can:
Drawbacks/missing features of the reset method of NGXS Store are:
Reset plugin improves all these because it:
How the concept of meta reducers related to all this is:
Run the following code in your terminal:
yarn add ngxs-reset-plugin
or if you are using npm:
npm install ngxs-reset-plugin
Import withNgxsResetPlugin
into your app config like:
import { withNgxsResetPlugin } from 'ngxs-reset-plugin';
export const appConfig: ApplicationConfig = {
providers: [
providerStore([]),
withNgxsResetPlugin()
]
}
To clear all states (on logout for example):
this.store.dispatch(
new StateClear()
);
To clear all states but one: *
this.store.dispatch(
new StateClear(SomeState)
);
To clear all states but some: *
this.store.dispatch(
new StateClear(SomeState, SomeOtherState)
);
* Keeping states while deleting others is useful especially combined with @ngxs/storage-plugin
To reset a single state to its defaults on certain events (such as route change):
this.store.dispatch(
new StateReset(SomeState)
);
To reset multiple states to their defaults (may prove useful in distributed scenarios):
this.store.dispatch(
new StateReset(SomeState, SomeOtherState)
);
To reset all states to their defaults:
this.store.dispatch(
new StateResetAll()
);
To reset all states to their defaults but keep one:
this.store.dispatch(
new StateResetAll(SomeState)
);
To reset all states to their defaults but keep some:
this.store.dispatch(
new StateResetAll(SomeState, SomeOtherState)
);
Important Note: Since it does not know anything about the actual states,
StateResetAll
resets the states based onInitState
action dispatched by NGXS at init. If you are persisting states with @ngxs/storage-plugin and want them to get reset as well, you need to reset those persisted states withStateReset
instead. Otherwise, next time the app starts, the storage plugin will put them in the store beforeInitState
andStateResetAll
will reset to initial states from storage and not to the original defaults of those states.
To replace the value of a single state with a new one: *
// type OverwriteTuple = [StateClass, any];
this.store.dispatch(
new StateOverwrite([SomeState, newStateValue])
);
To replace the value of multiple states with new ones: *
// type OverwriteTuple = [StateClass, any];
this.store.dispatch(
new StateOverwrite([SomeState, newStateValue], [SomeOtherState, newOtherStateValue])
);
* Overwriting states with improper values can break your application. Proceed with caution.
[x] Clear NGXS state(s) on dispatch of StateClear action
[x] Reset some NGXS state(s) to defaults on dispatch of StateReset action
[x] Reset all NGXS states to defaults to on dispatch of StateResetAll action
[x] Overwrite NGXS state(s) on dispatch of StateOverwrite action
[x] Test coverage
[x] Documentation & examples
[x] Permissive license
[x] Inclusive code of conduct
[x] Issue submission templates
[x] Contribution guidelines
[x] CI integrations