ngrx-store-reset
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

ngrx-store-reset

npm version

@ngrx/store meta reducer to reset the state.

Installation

npm i --save ngrx-store-reset

OR

yarn add ngrx-store-reset

Usage

1. Setup

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<State> = {
  // reducers
}
 
// Pass your action type (the one you'd like to reset the state) to the metareducer
export function storeResetMetaReducer(reducer: ActionReducer<State>): ActionReducer<State> {
  return storeReset({ action: ActionTypes.Logout })(reducer);
}
 
export const metaReducers: MetaReducer<State>[] = [ 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.

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<State>) {}
 
    logout() {
        this.store.dispatch(new Logout());
    }
}

Package Sidebar

Install

npm i ngrx-store-reset

Weekly Downloads

95

Version

0.0.3

License

MIT

Unpacked Size

5.71 kB

Total Files

6

Last publish

Collaborators

  • datencia