简体   繁体   English

使Redux减速器和其他非组件可热加载

[英]Make Redux reducers and other non-components hot loadable

I'm having a tough time getting my reducers to be hot swapable. 我很难让减压器变热。

I'm using Webpack and react-transform-hmr . 我正在使用Webpack和react-transform-hmr With this, all of the CSS and the components are hot loaded when I save, but when I try and work on another type of type - most notably the reducers - it would tell me to do a full refresh. 有了这个,当我保存时,所有的CSS和组件都是热加载的,但是当我尝试使用其他类型的类型时 - 最明显的是减速器 - 它会告诉我进行全面刷新。

I figured out that this is because I need to explicitly re-load the reducers in and accept the event. 我发现这是因为我需要明确地重新加载reducers并接受事件。 Which I'm doing with this code in my store.js : 我在store.js使用此代码:

if(module.hot) {
  module.hot.accept('./reducers/', () => {
    const nextRootReducer = require('./reducers/index');
    store.replaceReducer(nextRootReducer);
  });
}

reducers/index exports the root reducer. reducers/index导出根减速器。

However now when I run this it still tells me [HMR] Cannot check for update (Full reload needed and also errors saying [HMR] TypeError: currentReducer is not a function 但是现在当我运行它时它仍然告诉我[HMR] Cannot check for update (Full reload needed以及错误说[HMR] TypeError: currentReducer is not a function

So - I need some help getting this to work. 所以 - 我需要一些帮助才能让它发挥作用。 The code is available at https://github.com/wesbos/Simple-Redux and you can reproduce it by doing: 代码可以在https://github.com/wesbos/Simple-Redux上找到 ,你可以通过这样做来重现它:

  1. npm install
  2. npm start
  3. Open localhost:3000 in your browser 在浏览器中打开localhost:3000
  4. Edit a reducer - open posts.js and change the number on line 6 to anything else 编辑reducer - 打开posts.js并将第6行的数字更改为其他任何内容

I haven't looked closely but my best guess is that it's this issue . 我没有密切关注,但我最好的猜测就是这个问题
Babel 6 no longer tries to make ES6 default exports the result of module.exports . Babel 6不再尝试使ES6默认导出module.exports的结果。

So instead of 而不是

const nextRootReducer = require('./reducers/index');

you probably want 你可能想要的

const nextRootReducer = require('./reducers/index').default;

which matches the Babel 6 output for ES6 default exports. 它与Babel 6输出匹配,用于ES6默认导出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM