Skip to content

Commit

Permalink
Configure Hot Module Reloading for the React component tree
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Nov 9, 2016
1 parent dff01a8 commit ab73921
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,57 @@ import React from 'react';
import ReactDOM from 'react-dom';
import {Provider} from "react-redux";

import App from './App';
import './index.css';

import configureStore from "./store/configureStore";
const store = configureStore();

// Save a reference to the root element for reuse
const rootEl = document.getElementById("root");

// Create a reusable render method that we can call more than once
let render = () => {
// Dynamically import our main App component, and render it
const App = require("./App").default;

ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
rootEl
);
};

if(module.hot) {
// Support hot reloading of components
// and display an overlay for runtime errors
const renderApp = render;
const renderError = (error) => {
const RedBox = require("redbox-react").default;
ReactDOM.render(
<RedBox error={error} />,
rootEl,
);
};

// In development, we wrap the rendering function to catch errors,
// and if something breaks, log the error and render it to the screen
render = () => {
try {
renderApp();
}
catch(error) {
console.error(error);
renderError(error);
}
};

// Whenever the App component file or one of its dependencies
// is changed, re-import the updated component and re-render it
module.hot.accept("./App", () => {
setTimeout(render);
});
}

render();

ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);

0 comments on commit ab73921

Please sign in to comment.