Redux传奇从未上演过



我正在尝试在应用程序中配置redux-sagav1.1.3。

我在这里用README完成了下面的配置。

在我的项目中:

npm i redux-saga@1.1.3 --save

我创建了如下rootSaga.js

import { all } from 'redux-saga/effects'
export default (customSagas) => function* rootSaga() {
yield all([
...customSagas,
])
}

我把rootSaga.js变成了一个函数,因为rootSaga在我的应用程序的核心中,我需要能够向我的应用添加更多的customSaga。

这是我更新的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 };
}

在那里,我使用reduxredux-persist,我还配置了redux开发工具。并具有一些核心功能,如preferences被预配置和持久

这是我的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:中所说的传奇故事


function App() {
return (
<Button
push={() => dispatch({ type: 'LOGOUT' })}
>
Logout Saga
</Button>
);
}

我可以看到LOGOUT类型是在我的redux开发工具中调度的,但我看不到hello世界。

我哪里失败了?

rootSaga中执行yield all([...])时,必须调用类似sessionSaga()的函数。在您的代码中,您只是列出了生成器函数。参见根传奇文档

我想是这样的:

import { all, call } from 'redux-saga/effects'
export default (customSagas) => function* rootSaga() {
yield all(customSagas.map(saga => call(saga)));
}

可以工作。

相关内容

  • 没有找到相关文章

最新更新