-
Notifications
You must be signed in to change notification settings - Fork 311
How to get type and payload when dispatched? #259
Comments
Take a look at https://github.com/ngrx/effects, then subscribe to the |
I think you should do it using meta-reducer and localStorageSync. here is how you can do it: reducers.ts:
storageStore.ts:
|
The suggested solutions were too complicated. I ended up implementing a custom function that takes the store object, the payload, and a boolean for whether or not to save the store to local storage. Implementation: import { Store, Action } from '@ngrx/store';
export const dispatch = (store: Store<any>, action: Action, storeInLocalStorage = true) => {
store.dispatch(action);
if (storeInLocalStorage) {
store.first().subscribe((s: any) => {
let json = JSON.stringify(s);
localStorage.setItem('store', json);
});
}
}; Usage (e.g. in an Angular 2 component, into which the store has been injected): dispatch(this.store, { /* payload */ }, false); // true by default, will save to local storage For anyone stumbling upon this looking for a solution to a similar problem, this solution fixes this particular problem. However, I'm hesitant to close this issue as I think this solution is not universally applicable. Having access to the type and payload object whenever a dispatch occurs would have much further reach. |
why did you not consider @awerlang suggestion? I would definitely solve this with effects. |
@dchacke you're right, you've come up with a solution that doesn't scales. I'm afraid I was too synthetic at that time, and it wasn't enough for you to take a peek at
The logic around |
Please check this against NgRx v4, and if it’s still an issue, please reopen on https://github.com/ngrx/platform. Thanks! |
When a new dispatch comes through, how can one capture the type and the payload for that dispatch?
For example, I am subscribing to the store:
In almost all cases, I want to save the store to local storage. However, depending on the dispatched action and payload, there are cases in which I don't want to save it to local storage. Additional parameters, such as
action
andpayload
would be nice to determine this.In other words, I would like to be able to run
If there are other ways to do this, please point me in the right direction.
I have found a way to find the type and payload by subscribing to something like in https://github.com/ngrx/store-log-monitor/blob/master/src/log-monitor/log-monitor.ts#L87, but am finding it to be a bit much code for something this simple.
The text was updated successfully, but these errors were encountered: