未使用Redux输入的Redux Sagas持续存在并且已连接反应路由器

时间:2019-02-19 08:14:12

标签: reactjs redux redux-saga redux-persist connected-react-router

我有一个React Web应用程序,它使用redux,connected-react-router,redux saga和reduxpersist和HMR以及react-hot-loader和webpack。在对大多数软件包进行了重大更新之后,我注意到没有输入/执行sagas。

相关软件包的当前版本为: “ react”:“ ^ 16.7.0”,“ react-redux”:“ ^ 6.0.0”,“ redux”:“ ^ 4.0.1”,“ redux-persist”:“ 5.6.12”,“ redux- saga“:” ^ 1.0.1“,” react-hot-loader“:” ^ 4.6.5“,” connected-react-router“:” ^ 6.2.2“,” webpack“:” ^ 4.29.3“

我已经尝试将HMR实现从v4还原到更低的水平,但是我确信它可以正常工作。我也认为这可能是连接反应路由器的实现,但现在我对此也很有信心(但我将同时展示这两个参考)。我猜这是我的redux存储配置附近的事,但是我猜是否知道我不会寻求帮助。

index.js文件(应用程序入口点)

import React from 'react';
import ReactDOM from 'react-dom';
import { PersistGate } from 'redux-persist/integration/react';
import { Provider } from 'react-redux';
import App from './components/App';
import store, { persistor } from './store/config';

const render = (Component) => {
  ReactDOM.render(
    <Provider store={store}>
      <PersistGate persistor={persistor}>
        <Component />
      </PersistGate>
    </Provider>,
    document.getElementById('app'),
  );
};

render(App);

根减速器:

import { combineReducers } from 'redux';
import { connectRouter } from 'connected-react-router';
import { stateKeys } from '../types';
import authReducer from './auth/authReducer';

export default history => combineReducers({
  [stateKeys.ROUTER]: connectRouter(history),
  [stateKeys.AUTH]: authReducer,
});

root saga:

import watchAuthentication from './auth/sagas';

const root = function* rootSaga() {
  yield [
    watchAuthentication(),
  ];
};

export default root;

App.js(仅相关位):

import { hot } from 'react-hot-loader';
class App extends React.Component {
...
}
export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(App));

商店配置:

import {
  applyMiddleware,
  compose,
  createStore,
} from 'redux';
import createSagaMiddleware from 'redux-saga';
import { createMigrate, persistStore, persistReducer } from 'redux- 
persist';
import storage from 'redux-persist/lib/storage';
import reduxImmutableStateInvariant from 'redux-immutable-state- 
invariant';
import { createBrowserHistory } from 'history';
import { routerMiddleware } from 'connected-react-router';
import { manifest } from '../manifest';
import rootReducer from '../rootReducer';
import sagas from '../rootSaga';
import { stateKeys } from '../../types';


// persistence config
const persistConfig = {
  key: 'root',
  whitelist: [
    stateKeys.MANIFEST,
    stateKeys.VERSION,
  ],
  storage,
  migrate: createMigrate(manifest),
};

// Create and export the history object
export const history = createBrowserHistory();


// Middlewares setup
const reactRouterMiddleware = routerMiddleware(history);
const sagaMiddleware = createSagaMiddleware();

const middlewares = [];

// during development: enforce immutability and provide extended support for redux debugging tools.
let composeEnhancers = compose;

if (process.env.NODE_ENV === 'development') {
  middlewares.push(reduxImmutableStateInvariant());
  composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || 
composeEnhancers; // eslint-disable-line no-underscore-dangle, max-len
}

middlewares.push(sagaMiddleware, reactRouterMiddleware);

// create the redux store
const initialState = undefined;

const store = createStore(
  persistReducer(persistConfig, rootReducer(history)),
  initialState,
  composeEnhancers(applyMiddleware(...middlewares)),
);

// hot module replacement config
if (process.env.NODE_ENV === 'development' && module.hot) {
  module.hot.accept('../rootReducer', () => {
    const nextReducer = require('../rootReducer').default; // eslint-disable-line global-require
    store.replaceReducer(persistReducer(persistConfig, 
nextReducer(history)));
  });
}

// run the saga middleware
sagaMiddleware.run(sagas);

export const persistor = persistStore(store);
export default store;

Auth Saga:

import {
  call,
  take,
  takeLatest,
} from 'redux-saga/effects';
import * as actions from '../authActions';
import config from '../../../mockData/mock-config';

// Use mocked auth flow if mocked authentication is enabled in mock- 
   config.js.
   // If mocked auth is used, you can change the user object in the 
    mock-config.js
    const { authenticateFlow, signOutFlow } = (config.enabled && 
    config.mockAuthentication) ? require('./mockedAuthFlow') : 
    require('./authFlow');

console.log('Outside watchAuthentication: sagas are not 
running...why?');


export default function* watchAuthentication() {
  while(true) { // eslint-disable-line
    try {
      console.log('Inside watchAuthentication... we never get here, 
why? ');

      const loginAction = yield takeLatest(`${actions.login}`);
      yield call(authenticateFlow, loginAction);

      const signOutAction = yield take(`${actions.loginSignOut}`);

      yield call(signOutFlow, signOutAction);
    } catch (e) {
      console.warn('login flow failed');
    }
  }
}

我希望watchAuthentication内的控制台日志能够运行,但是永远不会。我认为问题出在商店配置附近,但在这一点上,我正在猜测和把握稻草,因为我不知道在哪里看。我知道这是一个复杂的问题,感谢任何人都能提供的帮助。在此先感谢!

1 个答案:

答案 0 :(得分:0)

与redux saga升级有关的问题是根saga。 我的解决方案是按以下方式使用yield:

import { all } from 'redux-saga/effects';
import watchAuthentication from './auth/sagas';

const root = function* rootSaga() {
  yield all([
    watchAuthentication(),
  ]);
};

export default root;

希望这可以帮助某人。