Skip to content

Commit 7fec01b

Browse files
committed
react hot realod with react-router@3
1 parent 81c3cab commit 7fec01b

File tree

4 files changed

+57
-16
lines changed

4 files changed

+57
-16
lines changed

src/App.jsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react'
2+
3+
import browserHistory from 'react-router/lib/browserHistory'
4+
import Router from 'react-router/lib/Router'
5+
6+
import routes from './routes'
7+
import { TCEmitter } from './helpers'
8+
import { EVENT_ROUTE_CHANGE } from './config/constants'
9+
10+
const onRouteChange = () => {
11+
TCEmitter.emit(EVENT_ROUTE_CHANGE, window.location.pathname)
12+
}
13+
14+
export default () => (
15+
<Router history={browserHistory} routes={routes} onUpdate={onRouteChange} />
16+
)

src/components/TopBar/ProjectsToolBar.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class ProjectsToolBar extends Component {
162162
shouldComponentUpdate(nextProps, nextState) {
163163
const { user, criteria, creatingProject, projectCreationError, searchTermTag } = this.props
164164
const { isCreateProjectModalVisible, errorCreatingProject, isFilterVisible } = this.state
165-
return nextProps.user.handle !== user.handle
165+
return (nextProps.user || {}).handle !== (user || {}).handle
166166
|| JSON.stringify(nextProps.criteria) !== JSON.stringify(criteria)
167167
|| nextProps.creatingProject !== creatingProject
168168
|| nextProps.projectCreationError !== projectCreationError
@@ -291,4 +291,4 @@ const mapStateToProps = ({ projectSearchSuggestions, searchTerm, projectSearch,
291291

292292
const actionsToBind = { projectSuggestions, loadProjects, createProjectAction }
293293

294-
export default connect(mapStateToProps, actionsToBind)(ProjectsToolBar)
294+
export default connect(mapStateToProps, actionsToBind)(ProjectsToolBar)

src/components/TopBar/TopBarContainer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class TopBarContainer extends React.Component {
2525
}
2626

2727
shouldComponentUpdate(nextProps) {
28-
return nextProps.user.handle !== this.props.user.handle
28+
return (nextProps.user || {}).handle !== (this.props.user || {}).handle
2929
|| nextProps.toolbar.type !== this.props.toolbar.type
3030
}
3131

src/index.jsx

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,13 @@ import 'babel-polyfill'
22
import React from 'react'
33
import { render } from 'react-dom'
44
import { Provider } from 'react-redux'
5-
import browserHistory from 'react-router/lib/browserHistory'
6-
import Router from 'react-router/lib/Router'
75

86
import _ from 'lodash'
97
import store from './config/store'
10-
import routes from './routes'
11-
import { TCEmitter } from './helpers'
12-
import { EVENT_ROUTE_CHANGE, SEGMENT_KEY } from './config/constants'
8+
import { SEGMENT_KEY } from './config/constants'
9+
import App from './App'
1310

1411
const mountNode = document.getElementById('root')
15-
const onRouteChange = () => {
16-
TCEmitter.emit(EVENT_ROUTE_CHANGE, window.location.pathname)
17-
}
1812

1913
/* eslint-disable */
2014
if (!_.isEmpty(SEGMENT_KEY)) {
@@ -25,8 +19,39 @@ if (!_.isEmpty(SEGMENT_KEY)) {
2519

2620
/* eslint-enable */
2721

28-
render((
29-
<Provider store={store}>
30-
<Router history={browserHistory} routes={routes} onUpdate={onRouteChange} />
31-
</Provider>
32-
), mountNode)
22+
const renderApp = (AppComponent) => {
23+
render((
24+
<Provider store={store}>
25+
<AppComponent />
26+
</Provider>
27+
), mountNode)
28+
}
29+
30+
renderApp(App)
31+
32+
/**
33+
* Warning from React Router, caused by react-hot-loader.
34+
* The warning can be safely ignored, so filter it from the console.
35+
* Otherwise you'll see it every time something changes.
36+
* See https://github.com/gaearon/react-hot-loader/issues/298
37+
*
38+
* I think if update to react-router it has to disappear
39+
*/
40+
if (module.hot) {
41+
const isString = (str) => typeof str === 'string'
42+
const orgError = console.error // eslint-disable-line no-console
43+
console.error = (...args) => { // eslint-disable-line no-console
44+
if (args && args.length === 1 && isString(args[0]) && args[0].indexOf('You cannot change <Router routes>;') > -1) {
45+
// React route changed
46+
} else {
47+
// Log the error as normally
48+
orgError.apply(console, args)
49+
}
50+
}
51+
52+
module.hot.accept('./App', () => {
53+
const AppComponent = require('./App').default
54+
55+
renderApp(AppComponent)
56+
})
57+
}

0 commit comments

Comments
 (0)