gpt4 book ai didi

javascript - saga 的高阶函数为 sagan 函数注入(inject)参数

转载 作者:行者123 更新时间:2023-11-29 20:35:06 25 4
gpt4 key购买 nike

我有许多使用全局参数 country 的 sagas,在每个 saga 中我都必须使用 reselect 从 store 中获取它。

const country = yield select(makeSelectCountry));
const filtersWithCountry = {
...filters,
country,
};

每次都是这样。然后我就用 filtersWithCountry 做 POST 请求。有什么方法可以制作一些高阶函数,例如在使用它时。它将为我们的 sagas 注入(inject) country 参数。

最佳答案

您只需像其他所有高阶函数一样准备它:

const withCountry = (saga) => {
return function*(...args) {
const country = yield select(makeSelectCountry());
yield saga(country, args);
};
};

现在你的 saga 应该是这样的:

function* exampleSaga (country) {
...
...
const filtersWithCountry = {
...filters,
country,
};
...
...
};

最后像这样使用它:

withCountry(exampleSaga)

关于javascript - saga 的高阶函数为 sagan 函数注入(inject)参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56928786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com