Skip to main content

Component Store

Demo

Demo-Code

Create a Store that lives in Your Component Lifecycle

app.component.ts
export interface MyState {
counter: number;
}

@Component()
export class CounterComponent implements OnDestroy {
private store = this.storeFactory.createComponentStore<MyState>({
storeName: 'BASIC_COUNTER',
defaultState: { counter: 0 },
});
public myStoreState$ = this.store.state$;

constructor(private storeFactory: StoreFactory) {}

ngOnDestroy() {
this.store.ngOnDestroy();
}
}
note

It's necessary to destroy your store after your component destroyed, to avoid side effects. Here you muss call the ngOnDestroy.