# ngrx-browser-storage-store

> Persist ngrx state to localStorage / sessionStrage

Latest version **1.0.6-1** (published 2020-05-03) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install ngrx-browser-storage-store
pnpm add ngrx-browser-storage-store
yarn add ngrx-browser-storage-store
bun add ngrx-browser-storage-store
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6-1 |
| Published | 2020-05-03 |
| First published | 2020-04-18 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 192.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Vladislav Hadzhiyski |
| Maintainers | hadzhiyski |
| Keywords | ngrx, store, local storage, session storage, storage, localStorage, sessionStorage, angular |

## Links

- npm: https://www.npmjs.com/package/ngrx-browser-storage-store
- Repository: https://github.com/hadzhiyski/ngrx-browser-storage-store
- Homepage: https://github.com/hadzhiyski/ngrx-browser-storage-store#readme
- Issues: https://github.com/hadzhiyski/ngrx-browser-storage-store/issues
- npm.io page: https://npm.io/package/ngrx-browser-storage-store

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 1.0.6-1 (latest) — 2020-05-03
- 1.0.6 — 2020-05-03
- 1.0.5 — 2020-04-21
- 1.0.4-1 — 2020-04-20
- 1.0.4 — 2020-04-20
- 1.0.3 — 2020-04-20
- 1.0.2 — 2020-04-20
- 1.0.1 — 2020-04-19
- 1.0.0 — 2020-04-18
- 0.1.1 — 2020-04-18

## README

# ngrx-browser-storage-store
Persist ngrx state to localStorage / sessionStrage

## Build Source
``` cli
  npm install
  ng build --project ngrx-browser-storage-store
```

## Getting Started

[Docs](https://hadzhiyski.github.io/ngrx-browser-storage-store/docs/)
[Demo](https://hadzhiyski.github.io/ngrx-browser-storage-store)

### Import BrowserStorageModule

#### For Root
To inject 'middleware' meta reducers, use the `META_REDUCERS` injection token exported in the `@ngrx/store` API and a `Provider` to register the meta reducers through dependency injection.
``` typescript
import { MetaReducer, META_REDUCERS } from '@ngrx/store';
import {
  BrowserStorage,
  BrowserStorageModule,
  BrowserStorageMetaReducerLoader,
} from 'ngrx-browser-storage-store';
import * as fromRoot from './reducers';

export function metaReducerFactory(bsLoader: BrowserStorageMetaReducerLoader): MetaReducer<fromRoot.State> {
  return bsLoader.get<fromRoot.State>();
}

@NgModule({
  imports: [
    // The options object is optional. If you do not provide options, the default values are used.
    BrowserStorageModule.forRoot({
      storage: BrowserStorage.SessionStorage // default: BrowserStorage.LocalStorage
    }),
    StoreModule.forRoot(reducers),
  ],
  providers: [
    {
      provide: META_REDUCERS,
      deps: [BrowserStorageMetaReducerLoader],
      useFactory: metaReducerFactory,
      multi: true,
    },
  ],
})
export class AppModule {}
```

#### For Feature
``` typescript
import { StoreModule, StoreConfig } from '@ngrx/store';
import {
  BrowserStorage,
  BrowserStorageModule,
  BrowserStorageMetaReducerLoader,
} from 'ngrx-browser-storage-store';
import * as fromCounter from './store/reducers/counter.reducer';

export const COUNTER_FEATURE_CONFIG = new InjectionToken<StoreConfig<fromCounter.ICounterState>>('Counter Feature Config');

export function getCounterFeatureConfig(bsLoader: BrowserStorageMetaReducerLoader): StoreConfig<fromCounter.ICounterState> {
  return {
    metaReducers: [
      bsLoader.get<fromCounter.ICounterState>()
    ]
  };
}

@NgModule({
  imports: [
    BrowserStorageModule.forFeature(fromCounter.counterFeatureKey, {
      storage: BrowserStorage.SessionStorage,
    }),
    StoreModule.forFeature(fromCounter.counterFeatureKey, fromCounter.reducer, COUNTER_FEATURE_CONFIG),
  ],
  providers: [
    {
      provide: COUNTER_FEATURE_CONFIG,
      deps: [BrowserStorageMetaReducerLoader],
      useFactory: getCounterFeatureConfig,
    },
  ],
})
export class CounterModule {}
```

### Save state to browser storage
You could save your state to localStorage / sessionStorage via `@ngrx/effects`

``` typescript
import { Injectable } from '@angular/core';
import { Actions } from '@ngrx/effects';
import { BrowserStorageEffects } from 'ngrx-browser-storage-store';
import { ICounterState } from '../reducers/counter.reducer';
import { selectCounterState } from '../selectors/counter.selectors';

@Injectable()
export class CounterEffects {
  saveToLocalStorage$ = this.browserStorageEffects.saveForFeature(
    this.actions$,
    selectCounterState
  );

  constructor(
    private actions$: Actions,
    private browserStorageEffects: BrowserStorageEffects<ICounterState>
  ) {}
}
```

### Examples & use cases
Take a look into the `ngrx-browser-storage-store-demo` application

## License
Licensed to [Apache-2.0](https://www.apache.org/licenses/LICENSE-2.0)

---
_Source: https://npm.io/package/ngrx-browser-storage-store · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
