0.0.2 • Published 4 years ago
@component-first/redux v0.0.2
A state management solution that implements the Redux pattern.
Usage
See basic examples below on usage:
npm install @component-first/reduxCreate an accompanying
component.store.tsfile containing your actions, reducers and effects:
import { ChangeDetectorRef, Injectable } from '@angular/core';
import { Store } from '@component-first/redux';
interface AppState {
title: string;
}
@Injectable()
export class AppStore extends Store<AppState> {
actions = {
updateTitle: this.createAction<{ title: string }>('updateTitle'),
};
selectors = {
title: this.select((state) => state.title),
};
create(cd: ChangeDetectorRef) {
this.init(cd, { title: 'Hello World' });
this.createReducer(this.actions.updateTitle, (state, { title }) => ({
...state,
title,
}));
this.createEffect(this.actions.updateTitle, () => {
console.log("We'll try update the title");
});
}
}- Provide it to your Component and inject it in your
constructorto use it:
export class AppComponent implements OnInit {
title: SelectorResult<string>;
constructor(private cd: ChangeDetectorRef, private store: AppStore) {
this.store.create(this.cd);
}
async ngOnInit() {
this.title = this.store.selectors.title;
setTimeout(() => {
this.store.dispatchAction(this.store.actions.updateTitle, {
title: 'Welcome to Component-First',
});
}, 3500);
}
}- Use the
latestpipe in ourtemplateto always ensure we have the latest value from the store rendered, even inOnPushcomponents (without sacrificing performance)
<h1>{{title | latest}}</h1>API
Coming Soon!