admin管理员组文章数量:1415697
I am a beginner at redux-sagas and I am confused with the following situation. I have an items array and I want to fork the same method for each item. So I am using this line of code:
yield items.map(item => fork(loadItemDetails, item));
With the above-mentioned code, loadItemDetails
is never invoked. On the contrary, if I call fork
individually on each item, as shown below, then it works as intended.
yield fork(loadItemDetails, items[0]);
yield fork(loadItemDetails, items[1]);
yield fork(loadItemDetails, items[2]);
This is confusing me and I can't figure out the reason why the map won't work.
I am a beginner at redux-sagas and I am confused with the following situation. I have an items array and I want to fork the same method for each item. So I am using this line of code:
yield items.map(item => fork(loadItemDetails, item));
With the above-mentioned code, loadItemDetails
is never invoked. On the contrary, if I call fork
individually on each item, as shown below, then it works as intended.
yield fork(loadItemDetails, items[0]);
yield fork(loadItemDetails, items[1]);
yield fork(loadItemDetails, items[2]);
This is confusing me and I can't figure out the reason why the map won't work.
Share Improve this question edited Mar 25, 2020 at 17:49 norbitrial 15.2k10 gold badges39 silver badges64 bronze badges asked Mar 25, 2020 at 17:43 Raafay AlamRaafay Alam 615 bronze badges4 Answers
Reset to default 7I came across the same issue.
items.forEach(item => yield fork(loadItemDetails, item));
won't work, as you will get the following error:
A 'yield' expression is only allowed in a generator body.
In order to resolve it, I used yield all
export function* itemDetailsSage() {
const { items } = yield take(SET_CART_ITEMS);
yield all(items.map(item => fork(loadItemDetails, item)));
}
It might be a little late but this should work.
for (let i = 0; i < items.length; i++) {
yield fork(loadItemDetails, items[i]);
}
I believe if you add yield
inside of the loop before the fork()
and instead of .map()
if you use .forEach()
then it should work like the other example what you have with separated fork
calls.
Try the following:
items.forEach(item => yield fork(loadItemDetails, item));
I hope this helps!
How about using afor of
:
for (let item of items) {
yield fork(loadItemDetails, item);
}
本文标签: javascriptfork with map function in Redux SagaStack Overflow
版权声明:本文标题:javascript - fork with map function in Redux Saga - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745238044a2649153.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论