react-boilerplate的示例sagas中,sagas作为数组导出:

export function* defaultSaga() {

}

export default [
  defaultSaga,
];


然后将默认值注入routes.js中:

...
injectSagas(sagas.default);
...


但是,当我添加新的身份验证路由时,即使我只有默认的sagas(与上述相同),也会出现错误消息saga argument must be a Generator function!

我将路线添加到routes.js

//routes.js
export function authRoutes(store) {
  //same code as createRoutes
}


并导入了该函数,以在Router中为app.js创建新路由:

//app.js
const rootRoute = {
  component: App,
  childRoutes: createRoutes(store),
};

const authRoute = {
  path: '/auth',
  component: Auth,
  childRoutes: authRoutes(store),
};

const routes = {
  childRoutes: [rootRoute, authRoute],
};


当我尝试将sagas.js的默认值编辑为:

export default function* root() {
  yield [
    defaultSaga(),
  ];
}


我收到错误injectAsyncSagas: Expected "sagas" to be an array of generator functions。所以我要做的是将sagas.default包装在数组的新路径中:

//routes.js - authRoutes(store)
importModules.then(([reducer, sagas, component]) => {
  injectReducer('login', reducer.default);
  injectSagas([sagas.default]);
  renderRoute(component);
});


为什么会出现这些错误,我该如何解决? (我是React和Generators的新手。)

最佳答案

哎呀我忘记了在接受包含生成器函数数组的测试时在store.runSaga(mySagas)中调用了store.js。我将其删除,错误消失了。

08-25 10:44