Skip to content

Commit 7e2fcda

Browse files
author
Sven
committed
feat: complete upgrade react-router v4
1 parent 3326ae9 commit 7e2fcda

File tree

11 files changed

+85
-90
lines changed

11 files changed

+85
-90
lines changed

src/components/AsyncComponent.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from 'react'
2+
3+
export default function asyncComponent (importComponent) {
4+
class AsyncComponent extends React.Component {
5+
constructor (props) {
6+
super(props)
7+
8+
this.state = {
9+
component: null,
10+
}
11+
}
12+
13+
async componentDidMount () {
14+
const { default : component } = await importComponent()
15+
16+
this.setState({
17+
component: component
18+
})
19+
}
20+
21+
render () {
22+
const C = this.state.component
23+
24+
return C
25+
? <C {...this.props} />
26+
: null
27+
}
28+
}
29+
30+
return AsyncComponent
31+
}

src/layouts/PageLayout/PageLayout.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import React from 'react'
2-
import { NavLink as Link, Switch, Route } from 'react-router-dom'
2+
import { NavLink as Link, Switch, Route, Redirect } from 'react-router-dom'
33
import { Collapse, Navbar, NavbarToggler, NavbarBrand, Nav, NavLink, Container } from 'reactstrap'
44
import PropTypes from 'prop-types'
55
import './PageLayout.scss'
6-
import Home from '../../routes/Home'
7-
import Counter from '../../routes/Counter'
6+
import Home, { AsyncCounter, AsyncZen, AsyncElapse, AsyncRoute, AsyncPageNotFound } from '../../routes'
87

98
const location = {
109
pathname : '/route/8080',
@@ -45,15 +44,20 @@ export default class PageLayout extends React.Component {
4544
<NavLink to='/zen' activeClassName='active' tag={Link}>Zen</NavLink>
4645
<NavLink to='/elapse' activeClassName='active' tag={Link}>Elapse</NavLink>
4746
<NavLink to={location} activeClassName='active' tag={Link}>Route</NavLink>
48-
<NavLink to='/notFound' activeClassName='active' tag={Link}>404</NavLink>
47+
<NavLink to='/pageNotFound' activeClassName='active' tag={Link}>404</NavLink>
4948
<NavLink href='https://github.com/YutHelloWorld/vortex-react'>Github</NavLink>
5049
</Nav>
5150
</Collapse>
5251
</Navbar>
5352
<Container className='text-center page-layout__viewport'>
5453
<Switch>
5554
<Route exact path='/' component={Home} />
56-
<Route path='/counter' component={Counter} />
55+
<Route path='/counter' component={AsyncCounter} />
56+
<Route path='/zen' component={AsyncZen} />
57+
<Route path='/elapse' component={AsyncElapse} />
58+
<Route path='/route/:id' component={AsyncRoute} />
59+
<Route path='/404' component={AsyncPageNotFound} />
60+
<Redirect from='*' to='/404' />
5761
</Switch>
5862
</Container>
5963
</div>

src/routes/Counter/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { injectReducer } from '../../store/reducers'
2+
import { store } from '../../main'
23
import Counter from './containers/CounterContainer'
34
import reducer from './modules/counter'
4-
import { store } from '../../main'
55

66
injectReducer(store, { key : 'counter', reducer })
7+
78
export default Counter

src/routes/Elapse/index.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import { injectReducer } from '../../store/reducers'
2+
import { store } from '../../main'
3+
import Elapse from './containers/ElapseContainer'
4+
import reducer from './modules/elapse'
25

3-
export default (store) => ({
4-
path : 'elapse',
5-
getComponent (nextState, cb) {
6-
require.ensure([], (require) => {
7-
const Elapse = require('./containers/ElapseContainer').default
8-
const reducer = require('./modules/elapse').default
9-
injectReducer(store, { key : 'elapse', reducer })
10-
cb(null, Elapse)
11-
}, 'elapse')
12-
}
13-
})
6+
injectReducer(store, { key : 'elapse', reducer })
7+
8+
export default Elapse

src/routes/PageNotFound/components/PageNotFound.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import './PageNotFound.scss'
55
import { withRouter } from 'react-router'
66
import { Button } from 'reactstrap'
77

8-
const PageNotFound = ({ router : { goBack } }) => (
8+
const PageNotFound = ({ history : { goBack } }) => (
99
<div className='not-found__container'>
1010
<p>Page not found!!!</p>
1111
<div>
@@ -16,7 +16,7 @@ const PageNotFound = ({ router : { goBack } }) => (
1616
)
1717

1818
PageNotFound.propTypes = {
19-
router : PropTypes.object.isRequired
19+
history : PropTypes.object.isRequired
2020
}
2121

2222
export default withRouter(PageNotFound)

src/routes/PageNotFound/index.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
export default () => ({
2-
path : '404',
3-
getComponent (nextState, cb) {
4-
require.ensure([], (require) => {
5-
const PageNotFound = require('./components/PageNotFound').default
6-
cb(null, PageNotFound)
7-
}, 'pageNotFound')
8-
}
9-
})
1+
import PageNotFound from './components/PageNotFound'
2+
3+
export default PageNotFound

src/routes/PageNotFound/redirect.js

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

src/routes/Route/components/Route.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
11
import React from 'react'
22
import PropTypes from 'prop-types'
3-
import { Link, withRouter } from 'react-router'
3+
import { withRouter } from 'react-router-dom'
44
import { Button } from 'reactstrap'
55

66
class Route extends React.Component {
77
static propTypes = {
8-
location : PropTypes.object.isRequired,
9-
params : PropTypes.object.isRequired,
10-
router : PropTypes.object.isRequired
8+
match : PropTypes.object.isRequired,
9+
history : PropTypes.object.isRequired
1110
}
1211

1312
constructor (props) {
1413
super(props)
1514
this.redirect = this.redirect.bind(this)
1615
}
1716

18-
redirect () {
19-
this.props.router.push('/form')
17+
redirect = () => {
18+
this.props.history.push('/form')
2019
}
2120

2221
render () {
23-
const { params, location } = this.props
24-
2522
return (
2623
<div>
2724
<div>
28-
<Link to='/test'>id: {params.id}</Link>
29-
<p>Path: {location.pathname} </p>
25+
<h3>id: {this.props.match.params.id}</h3>
26+
<p>Path: {this.props.match.url} </p>
3027
</div>
3128
<div>
3229
<Button color='link' style={{ cursor : 'pointer' }} onClick={this.redirect}>Go</Button>

src/routes/Route/index.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
export default (store) => ({
2-
path : 'route/:id',
3-
getComponent (nextState, cb) {
4-
require.ensure([], (require) => {
5-
const Route = require('./components/Route').default
6-
cb(null, Route)
7-
}, 'route')
8-
}
9-
})
1+
import Route from './components/Route'
2+
3+
export default Route

src/routes/Zen/index.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
import { injectReducer } from '../../store/reducers'
2+
import { store } from '../../main'
3+
import Zen from './containers/ZenContainer'
4+
import reducer from './modules/zen'
25

3-
export default (store) => ({
4-
path : 'zen',
5-
getComponent (nextState, cb) {
6-
require.ensure([], (require) => {
7-
const Zen = require('./containers/ZenContainer').default
8-
const reducer = require('./modules/zen').default
6+
injectReducer(store, { key : 'zen', reducer })
97

10-
injectReducer(store, { key : 'zen', reducer })
11-
12-
cb(null, Zen)
13-
}, 'zen')
14-
}
15-
})
8+
export default Zen

0 commit comments

Comments
 (0)