我应该如何解决这种副作用(redux saga)
How should I solve this kinda side-effect (redux saga)
我刚刚 运行 进入了一个小兔子洞,不知道如何解决这个问题 'nicely'。
我想要的:
- 我要派遣和ACTION,
- 我想根据它改变过滤器的减速器状态。
- 我想从 SAGA 中获取调度的 ACTION。
- 我想 select 来自 saga 的最新状态。 (这里是
兔子洞)
所以我怎么能说传奇要等到减速器发生变化,所以我可以 select 最新状态。 (当然我可以处理 reducer 和 saga 中的变化,但是有没有不同的方式,我只处理一次变化?)
// filters/actions.js
const changeFilterOption = (option, value) => ({
type: CHANGE_FILTER_OPTION,
payload: {
option,
value
}
})
// filters/reducer.js
reducer(state=initState, action) {
switch(action.type) {
//...
case CHANGE_FILTER_OPTION:
return changedFilterOptions...
//...
}
}
// listing/saga.js
saga(takeLatest(CHANGE_FILTER_OPTION, doSomeAPICallWithTheNewData))
function* doSomeAPICallWithTheNewData() {
//at this point the reducer might not changed yet,
//of course I can grab the payload and handle the changes in the saga as well.
//selectedOptions might not be updated...
const options = yield select(selectFilterOptions)
yield call(api.doSomeAPICall, option)
}
Sagas 将在 reducer 更新存储后接收操作。因此,您可以放心地假设您 select 来自商店的数据是最新的。
但是,在 sagaMiddleware 之前的流中的任何中间件不同步调用 next(action)
.
的极少数情况下,数据 "maybe" 会过时
参考资料
阅读官方文档中的块引用:
https://redux-saga.js.org/docs/api/index.html#selectselector-args
来自作者的GitHub条评论:
https://github.com/redux-saga/redux-saga/issues/148#issuecomment-187898044
源代码:
https://github.com/redux-saga/redux-saga/blob/master/packages/core/src/internal/middleware.js#L44
我刚刚 运行 进入了一个小兔子洞,不知道如何解决这个问题 'nicely'。
我想要的:
- 我要派遣和ACTION,
- 我想根据它改变过滤器的减速器状态。
- 我想从 SAGA 中获取调度的 ACTION。
- 我想 select 来自 saga 的最新状态。 (这里是 兔子洞)
所以我怎么能说传奇要等到减速器发生变化,所以我可以 select 最新状态。 (当然我可以处理 reducer 和 saga 中的变化,但是有没有不同的方式,我只处理一次变化?)
// filters/actions.js
const changeFilterOption = (option, value) => ({
type: CHANGE_FILTER_OPTION,
payload: {
option,
value
}
})
// filters/reducer.js
reducer(state=initState, action) {
switch(action.type) {
//...
case CHANGE_FILTER_OPTION:
return changedFilterOptions...
//...
}
}
// listing/saga.js
saga(takeLatest(CHANGE_FILTER_OPTION, doSomeAPICallWithTheNewData))
function* doSomeAPICallWithTheNewData() {
//at this point the reducer might not changed yet,
//of course I can grab the payload and handle the changes in the saga as well.
//selectedOptions might not be updated...
const options = yield select(selectFilterOptions)
yield call(api.doSomeAPICall, option)
}
Sagas 将在 reducer 更新存储后接收操作。因此,您可以放心地假设您 select 来自商店的数据是最新的。
但是,在 sagaMiddleware 之前的流中的任何中间件不同步调用 next(action)
.
参考资料
阅读官方文档中的块引用:
https://redux-saga.js.org/docs/api/index.html#selectselector-args
来自作者的GitHub条评论:
https://github.com/redux-saga/redux-saga/issues/148#issuecomment-187898044
源代码:
https://github.com/redux-saga/redux-saga/blob/master/packages/core/src/internal/middleware.js#L44