Redux传奇从未运行

问题描述 投票:0回答:1

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

我在下面的自述文件here中使用了我的配置。

在我的项目中:

npm i [email protected] --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开发工具中分派的,但是我看不到你好世界。

我在哪里失败?

javascript reactjs redux react-redux redux-saga
1个回答
1
投票

当您在yield all([...])中执行rootSaga时,您必须调用类似sessionSaga()的功能。在您的代码中,您只是列出了生成器函数。参见root saga docs

我认为:

import { all, call } from 'redux-saga/effects'

export default (customSagas) => function* rootSaga() {
  yield all(customSagas.map(saga => call(saga)));
}

可以。

© www.soinside.com 2019 - 2024. All rights reserved.