Skip to content

Commit 8c06d31

Browse files
committed
Refactor routes, containers, and main App component for react-router v4
1 parent 39ceb77 commit 8c06d31

File tree

16 files changed

+169
-151
lines changed

16 files changed

+169
-151
lines changed

client/index.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import 'babel-polyfill';
22
import React from 'react';
33
import ReactDOM from 'react-dom';
44
import { Provider } from 'react-redux';
5-
import { Router } from 'react-router';
6-
import { browserHistory } from 'react-router';
7-
import { syncHistoryWithStore } from 'react-router-redux';
5+
import { BrowserRouter } from 'react-router-dom';
6+
// import { browserHistory } from 'react-router';
7+
// import { syncHistoryWithStore } from 'react-router-redux';
88
import { AppContainer } from 'react-hot-loader';
99
import configureStore from 'store';
10-
import routes from 'routes';
10+
import App from 'containers/App';
1111

1212
/* Images
1313
* This space is reserved for images that are required by server rendering,
@@ -22,24 +22,26 @@ const rootElement = document.getElementById('app');
2222
// rendering.
2323
const initialState = window.__INITIAL_STATE__;
2424
const store = configureStore(initialState);
25-
const history = syncHistoryWithStore(browserHistory, store);
25+
// const history = syncHistoryWithStore(browserHistory, store);
2626

27-
const render = (routes) => {
27+
const render = (Component) => {
2828
ReactDOM.render(
2929
<Provider store={store}>
3030
<AppContainer>
31-
<Router history={history} routes={routes} />
31+
<BrowserRouter>
32+
<Component />
33+
</BrowserRouter>
3234
</AppContainer>
3335
</Provider>,
3436
rootElement
3537
);
3638
};
3739

38-
render(routes);
40+
render(App);
3941

4042
if (module.hot) {
41-
module.hot.accept('../common/js/routes', () => {
42-
const newRoutes = require('../common/js/routes').default;
43-
render(newRoutes);
43+
// We need to re-require the main App module.
44+
module.hot.accept('../common/js/containers/App', () => {
45+
render(require('../common/js/containers/App').default);
4446
});
4547
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 404.js
3+
* Renders a 404 page.
4+
*/
5+
6+
import React from 'react';
7+
import PropTypes from 'prop-types';
8+
9+
const ERROR_MESSAGES = {
10+
404: 'The page you requested was not found.',
11+
500: 'The server encountered an error.'
12+
};
13+
14+
const ErrorPage = (props) => {
15+
const { message } = props;
16+
17+
return (
18+
<div>
19+
<h1>Sorry!</h1>
20+
<p>{message}</p>
21+
</div>
22+
);
23+
};
24+
25+
ErrorPage.propTypes = {
26+
code: PropTypes.number,
27+
message: PropTypes.string
28+
};
29+
30+
ErrorPage.defaultProps = {
31+
code: 404,
32+
message: ERROR_MESSAGES[404]
33+
};
34+
35+
export default ErrorPage;

common/js/components/Header.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { Component } from 'react';
2-
import { Link } from 'react-router';
2+
import { Link } from 'react-router-dom';
33
import css from './Header.scss';
44

55
export default class Header extends Component {

common/js/components/Main.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react';
2+
import { Switch, Route } from 'react-router-dom';
3+
import routes from 'routes';
4+
5+
const Main = () => (
6+
<main>
7+
<Switch>
8+
{routes.map(route => <Route key={route.path} {...route} />)}
9+
</Switch>
10+
</main>
11+
);
12+
13+
export default Main;

common/js/containers/App/App.dev.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

common/js/containers/App/App.prod.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

common/js/containers/App/index.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
1-
if (process.env.NODE_ENV === 'production') {
2-
module.exports = require('./App.prod');
3-
} else {
4-
module.exports = require('./App.dev');
1+
import React, { Component } from 'react';
2+
import Main from 'components/Main';
3+
import Header from 'components/Header';
4+
import Footer from 'components/Footer';
5+
import DevTools from 'containers/DevTools';
6+
7+
export default class App extends Component {
8+
renderDevTools = () => {
9+
const { NODE_ENV, DISABLE_DEV_TOOLS } = process.env;
10+
11+
if (NODE_ENV === 'development' && !DISABLE_DEV_TOOLS) {
12+
return <DevTools />;
13+
}
14+
15+
return null;
16+
}
17+
render() {
18+
19+
return (
20+
<div>
21+
<Header />
22+
<Main />
23+
<Footer />
24+
{this.renderDevTools()}
25+
</div>
26+
);
27+
}
528
}

common/js/containers/Example.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@ import React, { Component } from 'react';
22
import { connect } from 'react-redux';
33
import ExampleComponent from 'components/Example';
44

5-
const mapStateToProps = (state, ownProps) => {
6-
return {
7-
example: state.example,
8-
location: ownProps.location,
9-
params: ownProps.params
10-
};
11-
};
12-
135
class ExampleContainer extends Component {
146
render() {
157
return (
@@ -21,4 +13,12 @@ class ExampleContainer extends Component {
2113
}
2214
}
2315

16+
const mapStateToProps = (state, ownProps) => {
17+
return {
18+
example: state.example,
19+
location: ownProps.location,
20+
params: ownProps.params
21+
};
22+
};
23+
2424
export default connect(mapStateToProps)(ExampleContainer);

common/js/containers/Home.js renamed to common/js/containers/Home/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import React, { Component } from 'react';
2+
import css from './index.scss';
23

34
export default class HomeContainer extends Component {
45
render() {
56
return (
6-
<div className="home">
7-
<h1>It works!</h1>
7+
<div className={css.home}>
8+
<h1>It Works!</h1>
89
</div>
910
);
1011
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.home {
2+
h1 {
3+
font-size: 50px;
4+
}
5+
}

0 commit comments

Comments
 (0)