添加react-hot-loader以弹出create-react-app
我正在使用此提交中的说明尝试添加 react-hot-loader
版本3到 create-react-app
。 (滚动到底部查看babel和webpack配置)
I am using the instructions from this commit to attempt to add react-hot-loader
version 3 to create-react-app
. (Scroll to bottom to see babel and webpack configs)
编辑:更改'webpack / hot / dev-server '
到'webpack / hot / only-dev-server'
允许热重新加载工作。为什么这样?另外,如果无法重新加载完整状态,我将如何重新加载网页?
Changing 'webpack/hot/dev-server'
to 'webpack/hot/only-dev-server'
allows hot reloading to work. Why so? Also, how would I make it reload the web page if the full state cannot be reloaded?
预期行为:
在编辑器中编辑React组件会替换浏览器中的模块而不刷新浏览器。
Editing a React component in the editor replaces the modules in the browser without refreshing the browser.
实际行为(已更改)配置):
在编辑器中编辑React组件会刷新浏览器,无论进行何种更改,都会显示更改。
Editing a React component in the editor refreshes the browser, no matter where the change is made, and displays that change.
我的代码:
我使用以下代码(如此提交)重新加载应用程序,包括Redux的提供商/商店。
I am using the following code (as suggested in this commit) to reload the application, including Provider/store from Redux.
import React from 'react'
import ReactDOM from 'react-dom'
import App from "./components/App/App"
import "./styles/index.scss"
import { AppContainer } from 'react-hot-loader'
import configureStore from "./redux/store"
const store = configureStore()
ReactDOM.render(
<div>
<AppContainer>
<App store={store} />
</AppContainer>
</div>,
document.getElementById('root')
)
if (module.hot) {
module.hot.accept('./components/App/App', () => {
render(
<AppContainer props={{ store }}>
{require('./components/App/App').default}
</AppContainer>,
document.getElementById('root')
)
})
}
我的配置:
原始配置来自 create-react-app
工具。更改后的配置是我尝试在此项目中使用 react-hot-loader
。
The original configs are from the create-react-app
tool. The altered configs are my attempts to get react-hot-loader
working in this project.
原始CRA webpack配置: https: //github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/config/webpack.config.dev.js
Original CRA webpack config: https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/config/webpack.config.dev.js
我改变的CRA webpack配置: https://gist.github.com/Connorelsea/674a31e157d54144623ebf0ec775e0e7
My altered CRA webpack config: https://gist.github.com/Connorelsea/674a31e157d54144623ebf0ec775e0e7
原始CRA babel配置: https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/config/babel.dev.js
Original CRA babel config: https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/config/babel.dev.js
我改变的CRA babel配置: https://gist.github.com/Connorelsea/58664bfea423f5708a2e0f168fd391c9
My altered CRA babel config: https://gist.github.com/Connorelsea/58664bfea423f5708a2e0f168fd391c9
花了半天时间,使用2017年10月的lates CRA - 1.0.14,它非常简单。删除所有更改并执行以下两项操作:
Spent half a day on it, using the lates CRA - 1.0.14, Oct 2017, it's terribly simple. Drop all changes and do two things:
1)添加到index.js
1) Add to index.js
if (module.hot) {
module.hot.accept();
}
2)稍微更新configureStore.js:
2) Update configureStore.js a bit:
if (module.hot && process.env.NODE_ENV !== 'production') {
module.hot.accept('./reducers', () => {
const nextRootReducer = require('./reducers'); // eslint-disable-line
store.replaceReducer(nextRootReducer);
});
}
return store;