Redux 传奇永远不会运行
Redux saga never runs
我正在尝试在我的应用程序中配置 redux-saga
v1.1.3。
我使用 README here 下面的配置。
在我的项目中:
npm i redux-saga@1.1.3 --save
我创建了 rootSaga.js
如下:
import { all } from 'redux-saga/effects'
export default (customSagas) => function* rootSaga() {
yield all([
...customSagas,
])
}
I turned the rootSaga.js
into a function, because the rootSaga is within the core of my application and I needed to be able to add more customSaga to my application.
这是我的更新 configureStore.js
:
import { routerMiddleware } from 'connected-react-router';
import {
createStore,
applyMiddleware,
compose,
} from 'redux';
import { persistReducer, persistStore } from 'redux-persist';
+ import createSagaMiddleware from 'redux-saga';
import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2';
import { augmentStore } from '@nfen/redux-reducer-injector';
import storage from './storage';
import combineAppReducers from '../reducers';
+ import rootSagas from '../sagas';
function setupCreateReducer(customReducers = {}, reducerConfig) {
return function createReducer() {
return combineAppReducers({
...customReducers,
}, reducerConfig);
};
}
export default function configureStore(customReducers, customSagas, initialState = {}, persistConfig = {}, reducerConfig = {}) {
const runtimePersistConfig = {
key: 'root',
stateReconciler: autoMergeLevel2,
storage,
...persistConfig,
whitelist: [
'preferences',
...(persistConfig.whitelist || []),
],
};
let composeEnhancers = compose;
const reduxSagaMonitorOptions = {};
/* istanbul ignore next */
if (__DEV__ && typeof window === 'object') { // eslint-disable-line no-undef
/* eslint-disable no-underscore-dangle */
if (window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) {
composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({});
}
/* eslint-enable */
}
+ const sagaMiddleware = createSagaMiddleware(reduxSagaMonitorOptions);
- const middlewares = combineAppMiddlewares([], reducerConfig.history);
+ const middlewares = combineAppMiddlewares([sagaMiddleware], reducerConfig.history);
- const middlewares = [routerMiddleware(reducerConfig.history)];
+ const middlewares = [sagaMiddleware, routerMiddleware(reducerConfig.history)];
const enhancers = [applyMiddleware(...middlewares)];
const createReducer = setupCreateReducer(customReducers, reducerConfig);
const persistedReducer = persistReducer(runtimePersistConfig, createReducer());
const store = createStore(
persistedReducer,
initialState,
composeEnhancers(...enhancers),
);
// Extensions
+ store.runSaga = sagaMiddleware.run;
+ store.runSaga(rootSagas(customSagas));
augmentStore(createReducer, store);
const persistor = persistStore(store);
return { store, persistor };
}
In there, I use redux
and redux-persist
, I also configure redux development tools. and have some core features such as preferences
being preconfigured and persistent
这是我的 customSagas.js
:
import sessionsSaga from './sessionsSaga';
export default [
sessionsSaga,
];
这是我的 sessionSaga.js
:
import { takeLatest } from 'redux-saga/effects';
export function* logout() {
console.log('hello world');
}
export default function* logoutSaga() {
yield takeLatest('LOGOUT' , logout);
}
这就是我在 JSX 中调用 saga 的方式:
function App() {
return (
<Button
push={() => dispatch({ type: 'LOGOUT' })}
>
Logout Saga
</Button>
);
}
我可以在我的 redux 开发工具中看到 LOGOUT
类型被调度,但是我看不到 hello world。
我哪里失败了?
当您在 rootSaga
中执行 yield all([...])
时,您必须 调用 类似 sessionSaga()
的函数。在您的代码中,您只是列出了生成器函数。参见 root saga docs
我觉得是这样的:
import { all, call } from 'redux-saga/effects'
export default (customSagas) => function* rootSaga() {
yield all(customSagas.map(saga => call(saga)));
}
可以工作。
我正在尝试在我的应用程序中配置 redux-saga
v1.1.3。
我使用 README here 下面的配置。
在我的项目中:
npm i redux-saga@1.1.3 --save
我创建了 rootSaga.js
如下:
import { all } from 'redux-saga/effects'
export default (customSagas) => function* rootSaga() {
yield all([
...customSagas,
])
}
I turned the
rootSaga.js
into a function, because the rootSaga is within the core of my application and I needed to be able to add more customSaga to my application.
这是我的更新 configureStore.js
:
import { routerMiddleware } from 'connected-react-router';
import {
createStore,
applyMiddleware,
compose,
} from 'redux';
import { persistReducer, persistStore } from 'redux-persist';
+ import createSagaMiddleware from 'redux-saga';
import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2';
import { augmentStore } from '@nfen/redux-reducer-injector';
import storage from './storage';
import combineAppReducers from '../reducers';
+ import rootSagas from '../sagas';
function setupCreateReducer(customReducers = {}, reducerConfig) {
return function createReducer() {
return combineAppReducers({
...customReducers,
}, reducerConfig);
};
}
export default function configureStore(customReducers, customSagas, initialState = {}, persistConfig = {}, reducerConfig = {}) {
const runtimePersistConfig = {
key: 'root',
stateReconciler: autoMergeLevel2,
storage,
...persistConfig,
whitelist: [
'preferences',
...(persistConfig.whitelist || []),
],
};
let composeEnhancers = compose;
const reduxSagaMonitorOptions = {};
/* istanbul ignore next */
if (__DEV__ && typeof window === 'object') { // eslint-disable-line no-undef
/* eslint-disable no-underscore-dangle */
if (window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) {
composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({});
}
/* eslint-enable */
}
+ const sagaMiddleware = createSagaMiddleware(reduxSagaMonitorOptions);
- const middlewares = combineAppMiddlewares([], reducerConfig.history);
+ const middlewares = combineAppMiddlewares([sagaMiddleware], reducerConfig.history);
- const middlewares = [routerMiddleware(reducerConfig.history)];
+ const middlewares = [sagaMiddleware, routerMiddleware(reducerConfig.history)];
const enhancers = [applyMiddleware(...middlewares)];
const createReducer = setupCreateReducer(customReducers, reducerConfig);
const persistedReducer = persistReducer(runtimePersistConfig, createReducer());
const store = createStore(
persistedReducer,
initialState,
composeEnhancers(...enhancers),
);
// Extensions
+ store.runSaga = sagaMiddleware.run;
+ store.runSaga(rootSagas(customSagas));
augmentStore(createReducer, store);
const persistor = persistStore(store);
return { store, persistor };
}
In there, I use
redux
andredux-persist
, I also configure redux development tools. and have some core features such aspreferences
being preconfigured and persistent
这是我的 customSagas.js
:
import sessionsSaga from './sessionsSaga';
export default [
sessionsSaga,
];
这是我的 sessionSaga.js
:
import { takeLatest } from 'redux-saga/effects';
export function* logout() {
console.log('hello world');
}
export default function* logoutSaga() {
yield takeLatest('LOGOUT' , logout);
}
这就是我在 JSX 中调用 saga 的方式:
function App() {
return (
<Button
push={() => dispatch({ type: 'LOGOUT' })}
>
Logout Saga
</Button>
);
}
我可以在我的 redux 开发工具中看到 LOGOUT
类型被调度,但是我看不到 hello world。
我哪里失败了?
当您在 rootSaga
中执行 yield all([...])
时,您必须 调用 类似 sessionSaga()
的函数。在您的代码中,您只是列出了生成器函数。参见 root saga docs
我觉得是这样的:
import { all, call } from 'redux-saga/effects'
export default (customSagas) => function* rootSaga() {
yield all(customSagas.map(saga => call(saga)));
}
可以工作。