Skip to content

Commit a602625

Browse files
justingreenbergDavid Zukowski
authored and
David Zukowski
committed
feat(core): explicitly unmount hot routes
1 parent 02e872b commit a602625

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

src/containers/AppContainer.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
import React, { PropTypes } from 'react'
1+
import React, { Component, PropTypes } from 'react'
22
import { Router } from 'react-router'
33
import { Provider } from 'react-redux'
44

5-
class AppContainer extends React.Component {
5+
class AppContainer extends Component {
66
static propTypes = {
77
history: PropTypes.object.isRequired,
88
routes: PropTypes.object.isRequired,
9-
routerKey: PropTypes.number,
109
store: PropTypes.object.isRequired
1110
}
1211

1312
render () {
14-
const { history, routes, routerKey, store } = this.props
13+
const { history, routes, store } = this.props
1514

1615
return (
1716
<Provider store={store}>
1817
<div style={{ height: '100%' }}>
19-
<Router history={history} children={routes} key={routerKey} />
18+
<Router history={history} children={routes} />
2019
</div>
2120
</Provider>
2221
)

src/main.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,37 +40,47 @@ if (__DEBUG__) {
4040
// ========================================================
4141
const MOUNT_NODE = document.getElementById('root')
4242

43-
let render = (routerKey = null) => {
43+
let render = () => {
4444
const routes = require('./routes/index').default(store)
4545

4646
ReactDOM.render(
4747
<AppContainer
4848
store={store}
4949
history={history}
5050
routes={routes}
51-
routerKey={routerKey}
5251
/>,
5352
MOUNT_NODE
5453
)
5554
}
5655

57-
// Enable HMR and catch runtime errors in RedBox
5856
// This code is excluded from production bundle
59-
if (__DEV__ && module.hot) {
60-
const renderApp = render
61-
const renderError = (error) => {
62-
const RedBox = require('redbox-react').default
57+
if (__DEV__) {
58+
if (module.hot) {
59+
// Development render functions
60+
const renderApp = render
61+
const renderError = (error) => {
62+
const RedBox = require('redbox-react').default
6363

64-
ReactDOM.render(<RedBox error={error} />, MOUNT_NODE)
65-
}
66-
render = () => {
67-
try {
68-
renderApp(Math.random())
69-
} catch (error) {
70-
renderError(error)
64+
ReactDOM.render(<RedBox error={error} />, MOUNT_NODE)
65+
}
66+
67+
// Wrap render in try/catch
68+
render = () => {
69+
try {
70+
renderApp()
71+
} catch (error) {
72+
renderError(error)
73+
}
7174
}
75+
76+
// Setup hot module replacement
77+
module.hot.accept('./routes/index', () => {
78+
setTimeout(() => {
79+
ReactDOM.unmountComponentAtNode(MOUNT_NODE)
80+
render()
81+
})
82+
})
7283
}
73-
module.hot.accept(['./routes/index'], () => render())
7484
}
7585

7686
// ========================================================

0 commit comments

Comments
 (0)