Incompatability with Babel 6 + Webpack Hot Reload
You can see a working example of this bug here: https://github.com/ohkaiby/webpack-reactredux-hotreload-bug
Basically store.replaceReducer() is passing in a babel6-transpiled reducer function, which is an object whose default property is the actual reducer function. However, replaceReducer (inside createStore.js) is expecting to run a function, so it ends up error'ing.
The problem is, I'm not sure whether this is a bug with Babel 6, babel-loader, or React-Redux. It seems that Babel is correctly transpiling local dependencies, but this results in an error where untranspiled ES5 code in React-Redux expecting to run a function. Therefore the real bug is with how Babel6-transpiled functions are passed vs. what untranspiled code is expecting to run.
Hi @ohkaiby, thanks for the report (and creating a working example :+1:). I am looking at babel 6 stuff this weekend so hopefully we can get this resolved too.
Just change
const updatedReducer = require('./reducer');
to
const updatedReducer = require('./reducer').default;
which matches the new semantics of Babel 6.
This was the breaking change in Babel 6: removing backward compatibility with CommonJS for default exports. They are now explicit and ES6-centric rather than trying-to-be-like-CommonJS-but-not-quite.
Or you can use this to support CommonJS default exports: https://github.com/59naga/babel-plugin-add-module-exports
Thanks guys, did not realize the ES6 update broke CommonJS compatability. babel-plugin-add-module-exports ended up working.
Thanks guys, did not realize the ES6 update broke CommonJS compatability.
It just changes semantics to match ES6 more closely. If you only develop an app (and not a library) just use require(...).default (or better ES6 :-) and don't worry.
if (module.hot) {
module.hot.accept('./reducer', () => {
const updatedReducer = require('./reducer');
createdStore.replaceReducer( updatedReducer );
});
}
Is this bit required at all when using webpack HMR? I cannot find the documentation for it or how it ended up in my code. Removing it does not appear to have effect either.
I can see it is required. HMR of reducers does not work without. I still cannot find the official docs reference.
@gajus The documentation is unfortunately not in the README, it is in one of the release notes for Redux: https://github.com/rackt/redux/releases/tag/v2.0.0
Thank you @ohkaiby
I can see it is required. HMR of reducers does not work without. I still cannot find the official docs reference.
This library is React Redux. It has no relation to HMR, especially for reducers. Therefore there is no reason why this information would be in README.
Sorry, @gaearon. Wrong thread to discuss the issue. Though I think @ohkaiby was talking about Redux documentation.
I couldn't find anything searching "site:rackt.org HMR", which would be useful. I came across this issue searching for "store.replaceReducer".
Yeah, we don’t really have docs for HMR at the moment. It would be nice to add as a “recipe”, contributions to docs are always welcome! https://github.com/rackt/redux/issues/721