admin 管理员组文章数量: 1086019
I am using Angular 6, ngrx/store, ngrx/effects.
I have an effect that should be triggered when i press "Save" button. I am using withLatestFrom
there to collect all data what i need for sending it to the server:
@Effect({dispatch: false})
saveAll$ = this.actions$.pipe(
ofType(ActionTypes.Save),
withLatestFrom(
this.store.select(fromReducers.getData1),
this.store.select(fromReducers.getData2),
this.store.select(fromReducers.getData3),
this.store.select(fromReducers.getData4)
),
switchMap(([action, data1, data2, data3, data4]: [ActionType, Data1[], Data2[], Data3[], Data4[]]) => {
// here is some operations with these data
return this.apiService.saveData({data1, data2, data3, data4})
})
)
Here is getData1
selector:
export const getData1= createSelector(
getItems,
getIndexes,
(items, indexes) => {
console.log('HI, I AM getData1');
return transformItems(items, indexes);
}
);
getItems
, in turn, return state.items
. The problem is that state.items
can be modified in another effect:
@Effect()
handleItemsChanges$ = this.actions$.pipe(
ofType(ActionTypes.ChangesInItems),
withLatestFrom(
this.store.select(fromReducers.getItems),
this.store.select(fromReducers.getUsers),
),
switchMap(([action, items, users]: [ActionType, Item[], User[]]) => {
console.log('I AM handleItemsChanges');
const actions = [];
if (itemsShouldBeUpdated) {
actions.push(new UpdateData(changes))
}
})
)
So getData1
selector gets data from the store depend on another effect named handleItemsChanges
. handleItemsChanges
effect is triggered every time something is changed related to the items and recalc it again.
As a result, in saveAll
i am getting not actual state.items
.
What am i doing wrong? May be i should use another operator insted of withLatestFrom
or what ca be the solution? Thank you
P.S. Btw i am using withLatestFrom
every time when i want to get some data from the store. Is it correct?
I am using Angular 6, ngrx/store, ngrx/effects.
I have an effect that should be triggered when i press "Save" button. I am using withLatestFrom
there to collect all data what i need for sending it to the server:
@Effect({dispatch: false})
saveAll$ = this.actions$.pipe(
ofType(ActionTypes.Save),
withLatestFrom(
this.store.select(fromReducers.getData1),
this.store.select(fromReducers.getData2),
this.store.select(fromReducers.getData3),
this.store.select(fromReducers.getData4)
),
switchMap(([action, data1, data2, data3, data4]: [ActionType, Data1[], Data2[], Data3[], Data4[]]) => {
// here is some operations with these data
return this.apiService.saveData({data1, data2, data3, data4})
})
)
Here is getData1
selector:
export const getData1= createSelector(
getItems,
getIndexes,
(items, indexes) => {
console.log('HI, I AM getData1');
return transformItems(items, indexes);
}
);
getItems
, in turn, return state.items
. The problem is that state.items
can be modified in another effect:
@Effect()
handleItemsChanges$ = this.actions$.pipe(
ofType(ActionTypes.ChangesInItems),
withLatestFrom(
this.store.select(fromReducers.getItems),
this.store.select(fromReducers.getUsers),
),
switchMap(([action, items, users]: [ActionType, Item[], User[]]) => {
console.log('I AM handleItemsChanges');
const actions = [];
if (itemsShouldBeUpdated) {
actions.push(new UpdateData(changes))
}
})
)
So getData1
selector gets data from the store depend on another effect named handleItemsChanges
. handleItemsChanges
effect is triggered every time something is changed related to the items and recalc it again.
As a result, in saveAll
i am getting not actual state.items
.
What am i doing wrong? May be i should use another operator insted of withLatestFrom
or what ca be the solution? Thank you
P.S. Btw i am using withLatestFrom
every time when i want to get some data from the store. Is it correct?
- You can write a single selector which gets you all the data. OR you can send all the data you need as payload. – Pavan Bahuguni Commented Jun 8, 2018 at 4:39
- You might be look this for solution. medium./@viestursv/… – chirag Commented Apr 1, 2020 at 8:23
2 Answers
Reset to default 2you need to have action handleItemsChanges
fired before saveAll
gets fired. One way to do it is to create an effect on handleItemsChanges
action and trigger the save
action.
The framework will guarantee the order of execution (handleItemsChanges
first then save
), this way the withLatestFrom
operation will work as you expected.
I've found discussion on ngrx github : https://github./ngrx/platform/issues/467 Looks like we have 2 ugly variants for accessing store from effects now.
本文标签: javascriptProper way to access store in ngrxeffectStack Overflow
版权声明:本文标题:javascript - Proper way to access store in ngrxeffect - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1743984910a2513682.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论