Skip to content

Commit ff2f92f

Browse files
roth1002timdorr
authored andcommitted
Upgrade to use react-router v4 for real-world example (#2593)
* Upgrade to use react-router v4 for react-router-dom with web * add jest-cli
1 parent 46c62b3 commit ff2f92f

File tree

12 files changed

+7054
-541
lines changed

12 files changed

+7054
-541
lines changed

examples/real-world/package-lock.json

Lines changed: 253 additions & 510 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/real-world/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
},
1212
"dependencies": {
1313
"humps": "^2.0.0",
14+
"jest-cli": "^20.0.4",
1415
"lodash": "^4.16.1",
1516
"normalizr": "^3.2.3",
1617
"prop-types": "^15.5.10",
1718
"react": "^15.5.0",
1819
"react-dom": "^15.5.0",
1920
"react-redux": "^5.0.5",
20-
"react-router": "^3.0.2",
21-
"react-router-redux": "^4.0.5",
21+
"react-router-dom": "^4.1.2",
2222
"redux": "^3.5.2",
2323
"redux-thunk": "^2.1.0"
2424
},

examples/real-world/src/components/Repo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
22
import PropTypes from 'prop-types'
3-
import { Link } from 'react-router'
3+
import { Link } from 'react-router-dom'
44

55
const Repo = ({ repo, owner }) => {
66
const { login } = owner

examples/real-world/src/components/User.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
22
import PropTypes from 'prop-types'
3-
import { Link } from 'react-router'
3+
import { Link } from 'react-router-dom'
44

55
const User = ({ user }) => {
66
const { login, avatarUrl, name } = user

examples/real-world/src/containers/App.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import React, { Component } from 'react'
44
import PropTypes from 'prop-types'
55
import { connect } from 'react-redux'
6-
import { browserHistory } from 'react-router'
6+
import { withRouter } from 'react-router-dom'
77
import Explore from '../components/Explore'
88
import { resetErrorMessage } from '../actions'
99

@@ -23,7 +23,7 @@ class App extends Component {
2323
}
2424

2525
handleChange = nextValue => {
26-
browserHistory.push(`/${nextValue}`)
26+
this.props.history.push(`/${nextValue}`)
2727
}
2828

2929
renderErrorMessage() {
@@ -62,6 +62,6 @@ const mapStateToProps = (state, ownProps) => ({
6262
inputValue: ownProps.location.pathname.substring(1)
6363
})
6464

65-
export default connect(mapStateToProps, {
65+
export default withRouter(connect(mapStateToProps, {
6666
resetErrorMessage
67-
})(App)
67+
})(App))

examples/real-world/src/containers/RepoPage.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import React, { Component } from 'react'
44
import PropTypes from 'prop-types'
55
import { connect } from 'react-redux'
6+
import { withRouter } from 'react-router-dom'
67
import { loadRepo, loadStargazers } from '../actions'
78
import Repo from '../components/Repo'
89
import User from '../components/User'
@@ -69,8 +70,8 @@ class RepoPage extends Component {
6970
const mapStateToProps = (state, ownProps) => {
7071
// We need to lower case the login/name due to the way GitHub's API behaves.
7172
// Have a look at ../middleware/api.js for more details.
72-
const login = ownProps.params.login.toLowerCase()
73-
const name = ownProps.params.name.toLowerCase()
73+
const login = ownProps.match.params.login.toLowerCase()
74+
const name = ownProps.match.params.name.toLowerCase()
7475

7576
const {
7677
pagination: { stargazersByRepo },
@@ -91,7 +92,7 @@ const mapStateToProps = (state, ownProps) => {
9192
}
9293
}
9394

94-
export default connect(mapStateToProps, {
95+
export default withRouter(connect(mapStateToProps, {
9596
loadRepo,
9697
loadStargazers
97-
})(RepoPage)
98+
})(RepoPage))
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
import React from 'react'
22
import PropTypes from 'prop-types'
33
import { Provider } from 'react-redux'
4-
import routes from '../routes'
54
import DevTools from './DevTools'
6-
import { Router } from 'react-router'
5+
import { Route } from 'react-router-dom'
6+
import App from './App'
7+
import UserPage from './UserPage'
8+
import RepoPage from './RepoPage'
79

8-
const Root = ({ store, history }) => (
10+
const Root = ({ store }) => (
911
<Provider store={store}>
1012
<div>
11-
<Router history={history} routes={routes} />
13+
<Route path="/" component={App} />
14+
<Route path="/:login/:name"
15+
component={RepoPage} />
16+
<Route path="/:login"
17+
component={UserPage} />
1218
<DevTools />
1319
</div>
1420
</Provider>
1521
)
1622

1723
Root.propTypes = {
1824
store: PropTypes.object.isRequired,
19-
history: PropTypes.object.isRequired
2025
}
2126

2227
export default Root
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import React from 'react'
22
import PropTypes from 'prop-types'
33
import { Provider } from 'react-redux'
4-
import routes from '../routes'
5-
import { Router } from 'react-router'
4+
import { Route } from 'react-router-dom'
5+
import App from './App'
6+
import UserPage from './UserPage'
7+
import RepoPage from './RepoPage'
68

7-
const Root = ({ store, history }) => (
9+
const Root = ({ store }) => (
810
<Provider store={store}>
9-
<Router history={history} routes={routes} />
11+
<Route path="/" component={App} />
12+
<Route path="/:login/:name"
13+
component={RepoPage} />
14+
<Route path="/:login"
15+
component={UserPage} />
1016
</Provider>
1117
)
1218

1319
Root.propTypes = {
1420
store: PropTypes.object.isRequired,
15-
history: PropTypes.object.isRequired
1621
}
1722
export default Root

examples/real-world/src/containers/UserPage.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import React, { Component } from 'react'
44
import PropTypes from 'prop-types'
55
import { connect } from 'react-redux'
6+
import { withRouter } from 'react-router-dom'
67
import { loadUser, loadStarred } from '../actions'
78
import User from '../components/User'
89
import Repo from '../components/Repo'
@@ -72,7 +73,7 @@ class UserPage extends Component {
7273
const mapStateToProps = (state, ownProps) => {
7374
// We need to lower case the login due to the way GitHub's API behaves.
7475
// Have a look at ../middleware/api.js for more details.
75-
const login = ownProps.params.login.toLowerCase()
76+
const login = ownProps.match.params.login.toLowerCase()
7677

7778
const {
7879
pagination: { starredByUser },
@@ -92,7 +93,7 @@ const mapStateToProps = (state, ownProps) => {
9293
}
9394
}
9495

95-
export default connect(mapStateToProps, {
96+
export default withRouter(connect(mapStateToProps, {
9697
loadUser,
9798
loadStarred
98-
})(UserPage)
99+
})(UserPage))

examples/real-world/src/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import React from 'react'
22
import { render } from 'react-dom'
3-
import { browserHistory } from 'react-router'
4-
import { syncHistoryWithStore } from 'react-router-redux'
3+
import { BrowserRouter as Router } from 'react-router-dom'
54
import Root from './containers/Root'
65
import configureStore from './store/configureStore'
76

87
const store = configureStore()
9-
const history = syncHistoryWithStore(browserHistory, store)
108

119
render(
12-
<Root store={store} history={history} />,
10+
<Router>
11+
<Root store={store} />
12+
</Router>,
1313
document.getElementById('root')
1414
)

examples/real-world/src/reducers/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as ActionTypes from '../actions'
22
import merge from 'lodash/merge'
33
import paginate from './paginate'
4-
import { routerReducer as routing } from 'react-router-redux'
54
import { combineReducers } from 'redux'
65

76
// Updates an entity cache in response to any action with response.entities.
@@ -50,7 +49,6 @@ const rootReducer = combineReducers({
5049
entities,
5150
pagination,
5251
errorMessage,
53-
routing
5452
})
5553

5654
export default rootReducer

0 commit comments

Comments
 (0)