0

Why should we put parameter into module.hot.accept()? If we put nothing, webpack.HotModuleReplacementPlugin works, but also React component will lose its state data. If we use react-hot-loader, we should put parameters to accept method, but I don't know what the first parameter means, and also why the callback should require again and get the default?

import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import {AppContainer} from 'react-hot-loader';

import getRouter from './src/router';

const boot = (elements)=> {
    ReactDOM.render(
        <AppContainer>
            {elements}
        </AppContainer>
        , document.getElementById('app'));
}

let rs = getRouter();
boot(rs); //first render

// render after source code change
if (module.hot) {
    module.hot.accept('./src/router', ()=> {
        boot(require('./src/router').default())
    });
}
glennsl
  • 28,186
  • 12
  • 57
  • 75

1 Answers1

0

I found the answer. The first parameter is a observed, it is a component, if it or its child component was changed, callback function(the second parameter) will be call, and we should require the component again and drop the old one, then render(replace with new one) the new component to the DOM.