gpt4 book ai didi

javascript - Redux - Saga,放置父函数

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:38:13 24 4
gpt4 key购买 nike

有一种方法可以在特定请求后在 saga 容器中调用 props 函数(由父组件传递,而不是在当前组件中传递)?

这是我的例子

saga.js

export function* getItem(options) {
try {
const url = `....`;
const response = yield call(request, url);
// here setResponse is an action that modify reducer and it's fine
yield put(setResponse(response));
// here I want to put a props parents function(something like this), but in sagas I dont't have access to props
yield put(this.props.parentFunction)
} catch (e) {
.....
}
}

我发现的唯一方法是使用回调(所以我调用 props 在 index.js 中运行并存在)或者直接将函数作为 getItems < strong>选项

有更清洁的方法吗?

最佳答案

我不确定我是否完全理解您的目标,但我遇到过类似的挑战 -

I need to run this function after my saga completes.

通常它是表示或组件相关的(例如隐藏微调器)。例如,Redux-Form 采用 Promise 创建的 submit 处理程序。我们如何将 saga 调用转化为 promise?

所以就像您建议的那样,我们可以传递函数。或者,我们可以转向另一个方向——“我的组件如何订阅 redux 事件?”

您绝对可以将适合您的用例的东西组合在一起,但我使用 redux-saga-thunk为了这。它允许 saga-running Action 调度调用返回一个 promise :this.props.runSaga().then(...

作为奖励,您还可以获得“派生状态”选择器,例如 pending(ACTION_NAME),这对于显示加载 UI 非常方便,无需手动管理 bool 标志。

关于javascript - Redux - Saga,放置父函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56527783/

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