首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未输入Redux Sagas与redux persist和connected-react-router

未输入Redux Sagas与redux persist和connected-react-router
EN

Stack Overflow用户
提问于 2019-02-19 16:14:12
回答 1查看 1K关注 0票数 4

我有一个使用redux,连接反应路由器,redux saga和redux坚持和HMR的反应网络应用程序,与反应热加载程序和webpack。在对大多数包进行重大更新后,我注意到这些传奇没有进入/执行。

相关包的当前版本为:"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 store配置中的一些东西,但我猜如果我知道我就不会寻求帮助了。

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

代码语言:javascript
复制
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);

根削减器:

代码语言:javascript
复制
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,
});

根传奇:

代码语言:javascript
复制
import watchAuthentication from './auth/sagas';

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

export default root;

App.js (仅相关位):

代码语言:javascript
复制
import { hot } from 'react-hot-loader';
class App extends React.Component {
...
}
export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(App));

存储配置:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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中的控制台日志能够运行,但它从来没有运行过。我认为问题出在商店配置上,但在这一点上,我猜测并抓住了救命稻草,因为我不知道到哪里去找。我知道这是一个复杂的问题,我很感谢任何人能提供的帮助。提前感谢!!

EN

回答 1

Stack Overflow用户

发布于 2020-05-10 04:49:27

关于redux saga升级的问题在root saga中。我的解决方案是使用yield,如下所示:

代码语言:javascript
复制
import { all } from 'redux-saga/effects';
import watchAuthentication from './auth/sagas';

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

export default root;

希望这能帮到别人。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54761512

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档