Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
173
rated 0 times [  178] [ 5]  / answers: 1 / hits: 16312  / 8 Years ago, tue, may 17, 2016, 12:00:00

I'm having a bit of trouble getting the react-hot webpack loader to work correctly.



When I load the page I get the following as I would expect:




[HMR] Waiting for update signal from WDS...

[WDS] Hot Module Replacement enabled.




But when I save a change the page automatically hard refreshes the browser (rather than a HMR replacement).



//webpack.config.js

{
entry: {
client: 'webpack-dev-server/client?http://localhost:8786', // WebpackDevServer host and port
app: ./HelloWorld.tsx
},
devtool: process.env.WEBPACK_DEVTOOL || 'cheap-module-source-map',
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].entry.js'
},
module: {
loaders: [
{
test: /.ts(x?)$/,
loaders: ['react-hot', 'babel-loader?cacheDirectory=true,presets[]=es2015,presets[]=react', 'ts-loader']
}
]
},
devServer: {
contentBase: ./dist,
port:8786
},
plugins: [
new webpack.NoErrorsPlugin()
]
}


command: webpack-dev-server --hot --inline



on an interesting sidenote if I use babel-preset-react-hmre everything works as expected. (However I don't really want to use this as it seems less supported than the proper react-hot loader).


More From » reactjs

 Answers
35

I just ran into this problem. A couple things:



To help debug your particular issue, try enabling Preserve log (in Chrome dev tools). This will persist console logs across page refreshes, so you'll at least be able to see any messages that webpack-dev-server is logging before it triggers a refresh.



"Preserve



In my case webpack-dev-server was refreshing because I had not opted into HMR in my entry js file. Adding the following line to the file solved the issue:



// Opt-in to Webpack hot module replacement
if (module.hot) module.hot.accept()


For details on the module.hot API the webpack HMR docs are very helpful.


[#62140] Sunday, May 15, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
braidenv

Total Points: 80
Total Questions: 104
Total Answers: 91

Location: Peru
Member since Fri, Oct 14, 2022
2 Years ago
;