Javascript Redux-Saga,put父函数
有一种方法可以在特定请求后在saga容器中调用props函数(由父组件传递,而不是在当前组件中传递) 这是我的例子 saga.js 我找到的唯一方法是使用回调(因此我调用props在index.js中运行并存在),或者直接将函数作为getItems选项传递Javascript Redux-Saga,put父函数,javascript,reactjs,redux-saga,Javascript,Reactjs,Redux Saga,有一种方法可以在特定请求后在saga容器中调用props函数(由父组件传递,而不是在当前组件中传递) 这是我的例子 saga.js 我找到的唯一方法是使用回调(因此我调用props在index.js中运行并存在),或者直接将函数作为getItems选项传递 有更干净的方法吗?我不确定我是否完全理解你的目标,但我也面临着类似的挑战- 我需要在我的传奇故事完成后运行这个函数 通常它与表示或组件相关(例如隐藏微调器)Redux表单,接受创建submit处理程序的承诺。我们如何将一个传奇故事转变为一个承
有更干净的方法吗?我不确定我是否完全理解你的目标,但我也面临着类似的挑战- 我需要在我的传奇故事完成后运行这个函数 通常它与表示或组件相关(例如隐藏微调器)<例如,code>Redux表单,接受创建
submit
处理程序的承诺。我们如何将一个传奇故事转变为一个承诺
正如你所建议的,我们可以传递函数。或者,我们可以转向另一个方向——“我的组件如何订阅redux事件?”
您完全可以将一些适用于您的用例的东西组合在一起,但我使用它。它允许saga运行操作分派调用返回一个承诺:this.props.runSaga()。然后(…
作为奖励,您还可以获得“派生状态”选择器,如
pending(ACTION\u NAME)
,这对于在不手动管理布尔标志的情况下显示加载UI非常方便。假设parentFunction
是纯函数。您可以通过将parentProps
作为选项
之外的参数传递给操作来实现这一点。以下是模式:
这里,getItemsAction
获取两个参数:
- 选项:一些数据
- parentProps:来自父组件的道具
这样,parentFunction可以存储在parentProps中,并在saga中访问。这
parentFunction
做什么?为什么只有在saga完成后才需要运行它?
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) {
.....
}
}
export const function getItemsAction(options, parentProps){
return ({ type: GET_ITEMS_REQUEST, payload: {options , parentProps} });
}
export function* getItemsSaga({ ...payload }) {
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(parentProps)
} catch (e) {
.....
}
}