【问题标题】:Redux-saga: Calling a saga outside the redux middleware environmentredux-saga:在 redux 中间件环境之外调用一个 saga
【发布时间】:2018-04-16 01:18:57
【问题描述】:

redux saga 的新手 - 我正试图弄清楚当 我可以访问商店时如何在 redux 中间件环境之外调用 saga。

阅读 redux saga 文档后,我有两个选择,要么调用 store.runSaga,要么使用 redux-saga 提供的 runSaga 实用程序。

这就是我想要做的:

步骤:

  1. 创建了一个暂停直到成功操作被调度的 saga。

类似这样的:

      export function* loadDashboardSequenced() {

      try {
      // Take pauses until action received.
      //Wait for the user to be loaded
      const user_success = yield take('FETCH_USER_SUCCESS');
       // ....do other stuff....
        } catch(error) {
         yield put({type: 'FETCH_FAILED', error: error.message});
      }
  1. 现在我尝试通过 store.runSaga https://redux-saga.js.org/docs/api/ 或 runSaga https://redux-saga.js.org/docs/api/index.html#runsagaoptions-saga-args 调用 saga

使用 runSaga 与 store.runSaga 有什么好处吗?我不确定此时我应该使用哪一个。有什么想法/建议吗?谢谢!

编辑:关于使用 runSaga https://redux-saga.js.org/docs/advanced/UsingRunSaga.html 的后续问题 这条线是什么意思

        subscribe: ..., // this will be used to resolve take Effects

【问题讨论】:

    标签: reactjs redux redux-thunk redux-saga


    【解决方案1】:

    store.runSaga 方法用于启动存储的根 saga。它还期望 redux-saga 成为 store 中间件:

    export default function configureStore(initialState) {
      const sagaMiddleware = createSagaMiddleware()
      return {
        ...createStore(reducer, initialState, applyMiddleware(/* other middleware, */sagaMiddleware)),
        runSaga: sagaMiddleware.run
      }
    }
    
    const store = configureStore()
    store.runSaga(rootSaga)
    

    另一方面,runSaga 方法用于将 redux-saga 连接到非存储对象和接口,这是你很少做的事情。

    总而言之,如果您需要 sagas puttake 方法来处理 redux 操作,那么您需要使用 redux-saga 作为 store 中间件。

    【讨论】:

      【解决方案2】:

      如果您有一个从 REST API 检索数据然后将数据交给 reducer 的 saga……那么在没有存储的情况下测试您的 saga 就很有意义。

      此外,我不喜欢Beginner Tutorial 测试的一点是,我想测试整个堆栈,从客户端到服务器。 “初学者教程”中的测试无法测试,因为它们实际上并没有得到 saga 的执行结果,只是得到了 call/put 消息。

      我会查看 The best way to test Redux Sagas 以进行完整讨论,但这里有一个总结 Phil Herbert 帖子的快速示例:

      import { runSaga } from 'redux-saga'; // using runSaga to execute the saga
      import { testMeSaga } from './sagas'; // the saga under test
      
      test('test testMeSaga by running it', async () => {
          // 'recordSaga' is a utility function for running sagas, see it below.
          const result = await recordSaga(
              testMeSaga,
              { payload: 'your input to the saga' }
          );
          expect(result.length).toBe(1);    
          // add more checks here ...
      });
      
      // this is the utility function that wraps the running of the saga
      async function recordSaga(saga, initialAction) {
          const dispatched = [];
          await runSaga(
              {
                  dispatch: (action) => dispatched.push(action)
              },
              saga,
              initialAction
          ).done;
      
          return dispatched;
      } 
      

      【讨论】:

        猜你喜欢
        • 2018-12-13
        • 2017-09-12
        • 2021-12-03
        • 2022-01-15
        • 2019-02-21
        • 2017-05-14
        • 2017-05-28
        • 2022-01-26
        • 2018-09-03
        相关资源
        最近更新 更多