redux-saga select() returns 未定义
redux-saga select() returns undefined
我无法让 redux-saga/effects select 方法像宣传的那样工作。我尝试了很多次尝试,这是我最简单的尝试使用 select 的示例,我希望它 return 我的状态,但 return 未定义。
Saga.js:
export function* selectTest() {
const temp = yield* select();
console.log('select returns: ', temp);
return temp;
}
export default [
selectTest,
]
在 routes.js 中(我正在使用 react-boilerplate)
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 return 秒后看到我的状态被打印出来,但它是 return 未定义的。我可以看到我的状态设置正确,其他组件可以使用它。
为什么 select() returning 状态没有?
您需要传入一个实际的选择器函数。最简单的例子是:
function* selectAndPrintState() {
const selectAllState = (state) => state;
const allState = yield select(selectAllState);
console.log(allState);
}
不使用yield *
,只使用yield select()
。
yield *
用于子生成器集成。
我无法让 redux-saga/effects select 方法像宣传的那样工作。我尝试了很多次尝试,这是我最简单的尝试使用 select 的示例,我希望它 return 我的状态,但 return 未定义。
Saga.js:
export function* selectTest() {
const temp = yield* select();
console.log('select returns: ', temp);
return temp;
}
export default [
selectTest,
]
在 routes.js 中(我正在使用 react-boilerplate)
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 return 秒后看到我的状态被打印出来,但它是 return 未定义的。我可以看到我的状态设置正确,其他组件可以使用它。
为什么 select() returning 状态没有?
您需要传入一个实际的选择器函数。最简单的例子是:
function* selectAndPrintState() {
const selectAllState = (state) => state;
const allState = yield select(selectAllState);
console.log(allState);
}
不使用yield *
,只使用yield select()
。
yield *
用于子生成器集成。