0.0.3 • Published 5 years ago

ngrx-store-reset v0.0.3

Weekly downloads
73
License
MIT
Repository
github
Last release
5 years ago

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());
    }
}