UNPKG

1.68 kBMarkdownView Raw
1## ngrx-store-freeze
2
3[![npm version](https://badge.fury.io/js/ngrx-store-freeze.svg)](https://badge.fury.io/js/ngrx-store-freeze)
4[![CircleCI](https://circleci.com/gh/brandonroberts/ngrx-store-freeze/tree/master.svg?style=svg&circle-token=6ba0f6b74d2186f7896a58377b8607346c07cee6)](https://circleci.com/gh/brandonroberts/ngrx-store-freeze/tree/master)
5
6ngrx-store-freeze is a meta-reducer that prevents state from being mutated
7
8* Recursively freezes the **current state**, the dispatched **action payload** if provided and the **new state**.
9* When mutation occurs, an exception will be thrown.
10* Should be used **only in development** to ensure that the state remains immutable.
11
12
13### Installation
14
15```sh
16npm i --save-dev ngrx-store-freeze
17```
18
19OR
20
21```sh
22yarn add ngrx-store-freeze --dev
23```
24
25### Setup
26
27```ts
28import { StoreModule, MetaReducer, ActionReducerMap } from '@ngrx/store';
29import { storeFreeze } from 'ngrx-store-freeze';
30import { environment } from '../environments/environment'; // Angular CLI environment
31
32export interface State {
33 // reducer interfaces
34}
35
36export const reducers: ActionReducerMap<State> = {
37 // reducers
38}
39
40export const metaReducers: MetaReducer<State>[] = !environment.production ? [storeFreeze]: [];
41
42@NgModule({
43 imports: [
44 StoreModule.forRoot(reducers, { metaReducers }),
45 ]
46})
47export class AppModule {}
48```
49
50## Additional Documentation
51
52* [Usage with `@ngrx/router-store`](./docs/docs.md#router-store-compatibility)
53
54## Credits
55
56[redux-freeze](https://github.com/buunguyen/redux-freeze) - Redux middleware that prevents state from being mutated
57[Attila Egyed](https://github.com/tsm91) - The original maintainer of this project