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
        • 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

Store

Store is observable object that holds all application state

PreviousImmutableJs SetupNextOperators

Last updated 6 years ago

Was this helpful?

Store can be accessed in one of 2 ways:

  • From

  • Injected directly into component via DI

@Component({
  selector: 'my-app',
  template: `
    <div>todos: {{ (todos | async)?.getIn([0]) }}</div>
  `
})
class MyAppComponent {
  todos: Observable<number>;

  constructor(private store: Store<AppState>){
    this.todos = store.select(['todos']);
  }
}

Also you can avoid having async pipe by subscribing to state change. But then you will be responsible for subscription management and view update when OnPush change detection is applied. Hence it is recommended to leave this for Angular.

@Component({
  selector: 'my-app',
  template: `
    <div>todos: {{ todos.getIn([0]) }}</div>
  `
})
class MyAppComponent {
  todos: Map<any, any>;
  subscription: Rx.Subscription;

  constructor(private store: Store<AppState>, cd: ChangeDetectorRef) implements OnDestroy {
    this.subscription = store.select(['todos'])
      .subscribe(state => {
        this.todos = state; // OR this.todos = state.toJS()
        cd.markForCheck();
      });
  }

  ngOnDestroy(){
    this.subscription.unsubscribe();
  }
}

TIP: try to avoid .toJS() calls because it is not the best practice and is might be costly performance vise.

Actions