redux-saga - redux-saga - 如何获取 watcher 方法 - takeEvery - 动作类型本身来更新 redux 存储
问题描述
让我们以初学者文档为例:
https://redux-saga.js.org/docs/introduction/BeginnerTutorial.html
import { put, takeEvery, all } from 'redux-saga/effects'
const delay = (ms) => new Promise(res => setTimeout(res, ms))
function* helloSaga() {
console.log('Hello Sagas!')
}
function* incrementAsync() {
yield delay(1000)
yield put({ type: 'INCREMENT' })
}
function* watchIncrementAsync() {
yield takeEvery('INCREMENT_ASYNC', incrementAsync)
}
// notice how we now only export the rootSaga
// single entry point to start all Sagas at once
export default function* rootSaga() {
yield all([
helloSaga(),
watchIncrementAsync()
])
}
reducer 需要检测 INCREMENT_ASYNC,即使此操作类型上的任何内容都没有实际更改状态。否则我们会得到错误:
错误 takeLatest$1 需要模式或通道
无论如何,这很好,只是让一个额外的类型在减速器本身什么都不做似乎很浪费。
可以说我宁愿有 1 种类型 - INCREMENT_ASYNC,然后删除 INCREMENT。
我想用 INCREMENT_ASYNC 填充我的商店。如何模拟此代码以从 incrementAsync 提取响应以更新 INCREMENT_ASYNC 操作类型?
解决方案
推荐阅读
- xml - 如何使用 Java SAX 验证器找到有问题的 XML Schema?
- python - networkX 创建拓扑
- python - Python List 接受 None 和特定类型但不是 Any
- python - 在 Mac Catalina 上安装 Opencv
- scala - 在 Scala 中按值对现有映射进行排序
- reactjs - 如何修复 material-ui 中的文本/数据溢出?
- django - Django - 在一个模板中显示多个视图
- c# - 如何将我的数据放入 PickerViewModel
- elasticsearch - Elasticsearch:将 copy_to 与子对象一起使用的正确方法是什么
- python - 以编程方式调用网站时无法握手网站