react-hot-loader: react-hot-loader not work

Description

I use react-hot-loader and webpack for HMR, i use hot for App , but when i save the code , hot reload not work , how i change ?

Environment

node: 8.9.0 react-hot-loader: 4.1.1 webpack :4.6.0 webpack-dev-server: 3.1.3

Code

webpack.config.js

module.exports = {
  entry: {
    main: [
      'babel-polyfill',
      path.resolve(__dirname, '../src/main.js')
    ]
  },
  output: {
    path: path.resolve(__dirname, '../dist'),
    publicPath: './',
    filename: '[name].js',
    chunkFilename: 'chunk/[name].[chunkhash].js'
  }
}

// --------

config.entry.main = (config.entry.main || []).concat([
  'react-hot-loader/patch',
  `webpack-dev-server/client?http://localhost:${PORT}/`,
  'webpack/hot/dev-server'
])
config.plugins = (config.plugins || []).concat([
  new webpack.HotModuleReplacementPlugin()
])
config.mode = 'development'

const compiler = webpack(config)

const server = new WebpackDevServer(compiler, {
  hot: true,
  noInfo: true,
  quiet: true,
  filename: config.output.filename,
  publicPath: '/',
  stats: {
    colors: true
  }
})

App.js

import { hot } from 'react-hot-loader'

import RouterLink from './views/RouterLink'

class App extends Component {
  render () {
    return <RouterLink />
  }
}

export default hot(module)(App)

store.js

const configureStore = () => {
  const store = createStore(
    reducers,
    applyMiddleware(thunk, middleware)
  )

  if (process.env.NODE_ENV !== 'production'){
    if (module.hot) {
      module.hot.accept('../reducers', () => {
        store.replaceReducer(reducers)
      })
    }
  }
  return store
}

main.js

import configureStore from './store'

const history = createHistory()
export const store = configureStore()

ReactDOM.render((
  <Provider store={store}>
    <ConnectedRouter history={history}>
      <App />
    </ConnectedRouter>
  </Provider>
), document.getElementById('app'))

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 25

Most upvoted comments

No, only for the ones you are dynamically import. Hot loader could not update components you have to “re”-import first. So - ok make them self-reloadable (wrap with hot), or use RHL-friendly loader.

Sorry for a delay. Only now I got some time to have a look. Everything is simple, and described in readme. Not very clearly, but described.

https://github.com/gaearon/react-hot-loader#code-splitting

As long your components are updated, not remounter lifeCycle of your asyncLoader will not be triggered. There is 3 ways to fix the problem:

  1. Export each “exported” component (the one you will “import” later) with hot. Then it will “reload” itself.
  2. Use hot-loader compatible loader - loadable-components or react-imported-component
  3. Patch your own imported Component to become “compatible”. Unfortunately, with React 16.3, this is not as simple as before.

Better stick to first 2 options.