首页 > 解决方案 > 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 操作类型?

标签: redux-saga

解决方案


推荐阅读