HMR之后不再调用MapStateToProps

时间:2019-06-10 10:34:00

标签: redux react-redux redux-saga hot-module-replacement

我有一个将React,redux和redux sage与HMR(HotModuleReplacementPlugin)结合使用进行开发的应用程序。 更改源文件后,我的应用程序会自动更新:

[HMR] Updated modules:
log.js?1afd:24 [HMR]  - ./src/app/app.jsx
log.js?1afd:24 [HMR]  - ./src/app/index.jsx
log.js?1afd:24 [HMR] App is up to date.

但是在那之后,我不能再使用它了,因为不再调用mapStateToProps了。但是,我可以看到该操作已被触发并且更改传播到状态(redux开发工具),但是组件未从新状态接收更改。

可以通过在整个页面上重新加载F5来解决此问题,但它有点遗漏了HMR。

import { createStore, applyMiddleware } from 'redux';
import createSagaMiddleware from 'redux-saga';

import reducer from './reducer';
import sagas from './sagas';

import { composeEnhancers } from './redux-devtools';

// create the saga middleware
const sagaMiddleware = createSagaMiddleware();

// create the store
export const store = createStore(
  reducer,
  composeEnhancers(applyMiddleware(
    sagaMiddleware,
  )),
);

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

if (module.hot) {
  module.hot.accept('./reducer', () => {
    // Quite ugly: this piece of code must use AMD because it will be run in the built
    // environment.
    // eslint-disable-next-line
    require(["./reducer"], function (nextReducer) {
      store.replaceReducer(nextReducer.default);
    });
  });
}

export default store;

使用过的软件包:

“ react-redux”:“ ^ 6.0.1”,
“ redux-devtools-extension”:“ ^ 2.13.8”,
“ redux-saga”:“ ^ 1.0.1”,
“ react”:“ ^ 16.5.2”,

0 个答案:

没有答案