https://github.com/datencia/ngrx-store-reset
@ngrx/store meta reducer to reset the state.
https://github.com/datencia/ngrx-store-reset
angular ngrx ngrx-store reducer redux reset
Last synced: about 19 hours ago
JSON representation
@ngrx/store meta reducer to reset the state.
- Host: GitHub
- URL: https://github.com/datencia/ngrx-store-reset
- Owner: datencia
- License: mit
- Created: 2019-05-04T11:12:19.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-05-01T01:27:22.000Z (over 2 years ago)
- Last Synced: 2025-04-21T05:07:28.194Z (6 months ago)
- Topics: angular, ngrx, ngrx-store, reducer, redux, reset
- Language: TypeScript
- Size: 198 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ngrx-store-reset
[](https://badge.fury.io/js/ngrx-store-reset)
@ngrx/store meta reducer to reset the state.
## Installation
```sh
npm i --save ngrx-store-reset
```OR
```sh
yarn add ngrx-store-reset
```## Usage
### 1. Setup
```ts
import { StoreModule, MetaReducer, ActionReducerMap } from '@ngrx/store';
import { storeReset } from 'ngrx-store-reset';import { ActionTypes } from './actions';
export interface State {
// reducer interfaces
}export const reducers: ActionReducerMap = {
// reducers
}// Pass your action type (the one you'd like to reset the state) to the metareducer
export function storeResetMetaReducer(reducer: ActionReducer): ActionReducer {
return storeReset({ action: ActionTypes.Logout })(reducer);
}export const metaReducers: MetaReducer[] = [ storeResetMetaReducer ];
@NgModule({
imports: [
StoreModule.forRoot(reducers, { metaReducers }),
]
})
export class AppModule {}
```#### Options
- `action`: specify the action to listen for to reset the state (defaults to `RESET_STORE`).
### 2. Resetting the state
Just dispatch your action, the metareducer will do the job.
```ts
import { Component } from '@angular/core';
import { Store } from '@ngrx/store';import { Logout } from './actions';
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.scss']
})
export class MyComponentComponent {
constructor(private store: Store) {}logout() {
this.store.dispatch(new Logout());
}
}
```