ng-state
  • Introduction
  • Main differences
  • Getting Started
    • Instalation
    • Examples
  • Core concepts
    • Main idea
      • More complex flow visualization
    • Configuration
      • Immer Setup
      • ImmutableJs Setup
    • Store
      • Operators
      • Optimistic updates plugin
      • Optimistic updates plugin
      • Form manager plugin
        • onChange hook
        • onPropertyChange
        • shouldUpdateState hook
        • Custom form elements
      • Persist state plugin
        • Configuring custom storage
    • Actions
      • Immer
      • ImmutaleJs
      • Injectable Actions
    • Components with Actions
      • Signal Actions
      • State Actions
    • @InjectStore decorator
    • @WithStore decorator
    • @ComponentState decorator
    • Dispatcher
  • Different scenarios
    • Passing list item index via router
    • ngOnChanges hook
    • FormManager pitfalls
  • Unit testing
    • Setup
    • Test store
    • Test actions
    • Test component with actions
    • Test with Angular TestBed
  • Debugging
    • Setup
    • Redux DevTools
    • Automated changes output
    • Manual state changes check
    • Additional debugging information
  • Production
    • Production mode
    • Server Side Rendering (SSR)
  • Other information
    • Best practices
    • CLI
      • Custom Configurations
    • Performance
    • Blog Posts
    • Contributing
Powered by GitBook
On this page

Was this helpful?

  1. Core concepts
  2. Store
  3. Persist state plugin

Configuring custom storage

By default localStorage is used. However you can define other storages in one of two ways:

  • Setting it globally by calling PersistStateManager.configureStorage({...}) - passed object should contain clear, getItem, removeItem, setItem methods and method to gather keys from storage () => Object.keys(sessionStorage)

PersistStateManager.configureStorage({
    clear: () => timer(2000).pipe(tap(_ => localStorage.clear())),
    getItem: (key: string) => timer(2000).pipe(map(_ => localStorage.getItem(key))),
    removeItem: (key: string) => timer(2000).pipe(tap(_ => localStorage.removeItem(key))),
    setItem: (key: string, value: any) => timer(2000).pipe(tap(_ => localStorage.setItem(key, value))),
    }, () => timer(2000).pipe(map(_ => Object.keys(localStorage))));
  • passing to params when calling save method for example:

this.store.select(['todos']).storage.save({ 
    storageConfig: { 
        storage: sessionStorage, getKeys: Object.keys(sessionStorage) 
        } 
    });

PreviousPersist state pluginNextActions

Last updated 6 years ago

Was this helpful?