代码之家  ›  专栏  ›  技术社区  ›  dsollen

redux saga select()返回未定义的

  •  0
  • dsollen  · 技术社区  · 9 年前

    我无法让redux saga/effects select方法像广告中那样工作。我已经尝试了很多次,下面是我最简单的使用select的例子,我希望返回我的状态,但返回未定义的。

    传奇故事js:

    export function* selectTest() {
      const temp = yield* select();
      console.log('select returns: ', temp);
      return temp;
    }
    
    export default [
      selectTest,
    ]
    

    return [
     {
        path,
        name: 'home',
        getComponent(nextState, cb) {
          const importModules = Promise.all([
              System.import('containers/UiComponentsHomePage/sagas')
              System.import('containers/UiComponentsHomePage'),
          ]);
    
        const renderRoute = loadModule(cb);
    
        importModules.then(([saga, component]) => {
          injectSagas(saga.default);
          renderRoute(component);
        });
    
        importModules.catch(errorLoading);
       },
      }
     ];
    

    我希望看到我的状态在select返回后打印出来,但它返回的是未定义的。我可以看到我的状态设置正确,其他组件可以使用它。

    2 回复  |  直到 9 年前
        1
  •  2
  •   cyrilluce    9 年前

    不要使用 yield * yield select() . 用于子生成器集成。

        2
  •  0
  •   markerikson    9 年前

    您需要传入一个实际的选择器函数。最简单的例子是:

    function* selectAndPrintState() {
        const selectAllState = (state) => state;
    
        const allState = yield select(selectAllState);
        console.log(allState);
    }