使Redux减速器和其他非组件可热加载 [英] Make Redux reducers and other non-components hot loadable

查看:60
本文介绍了使Redux减速器和其他非组件可热加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难让减速器热插拔.

I'm having a tough time getting my reducers to be hot swapable.

我正在使用Webpack和react-transform-hmr.这样,保存时所有CSS和组件都会被热加载,但是当我尝试使用另一种类型的类型(最著名的是reducer)时,它将告诉我进行完全刷新.

I'm using Webpack and 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.

我发现这是因为我需要显式地重新加载化简器并接受事件.我正在使用我的store.js:

I figured out that this is because I need to explicitly re-load the reducers in and accept the event. Which I'm doing with this code in my store.js:

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

reducers/index导出根减速器.

但是现在当我运行它时,它仍然告诉我[HMR] Cannot check for update (Full reload needed,并且还提示[HMR] TypeError: currentReducer is not a function

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

所以-我需要一些帮助以使其正常工作.该代码位于 https://github.com/wesbos/Simple-Redux 上,您可以通过执行以下操作来重现它:

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:

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

推荐答案

我没有仔细看,但我最大的猜测是它是

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.

所以不是

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

您可能想要

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

与Babel 6输出匹配以进行ES6默认导出.

which matches the Babel 6 output for ES6 default exports.

这篇关于使Redux减速器和其他非组件可热加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆