Skip to content

Commit a1f74f6

Browse files
Merge pull request #20 from YutHelloWorld/feature
feat: upgrade to react-router v4
2 parents afb554d + 358b8de commit a1f74f6

File tree

21 files changed

+168
-199
lines changed

21 files changed

+168
-199
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"react": "^15.5.4",
3131
"react-dom": "^15.5.4",
3232
"react-redux": "^5.0.4",
33-
"react-router": "^3.0.0",
33+
"react-router-dom": "^4.1.2",
3434
"react-spinkit": "^3.0.0",
3535
"react-transition-group": "^1.1.2",
3636
"reactstrap": "^4.8.0",

src/components/App.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import React from 'react'
2-
import { browserHistory, Router } from 'react-router'
2+
import { Router, Route } from 'react-router-dom'
33
import { Provider } from 'react-redux'
44
import PropTypes from 'prop-types'
5+
import CoreLayout from '../layouts/PageLayout/PageLayout'
6+
import { history } from '../store/location'
57

68
class App extends React.Component {
79
static propTypes = {
8-
store : PropTypes.object.isRequired,
9-
routes : PropTypes.object.isRequired
10+
store : PropTypes.object.isRequired
1011
}
1112

1213
shouldComponentUpdate () {
@@ -15,12 +16,16 @@ class App extends React.Component {
1516

1617
render () {
1718
/* ES6 解构赋值 */
18-
const { store, routes } = this.props
19+
const { store } = this.props
1920

2021
return (
2122
<Provider store={store}>
2223
<div style={{ height : '100%' }}>
23-
<Router history={browserHistory} children={routes} />
24+
<Router history={history}>
25+
<div>
26+
<Route path='/' component={CoreLayout} />
27+
</div>
28+
</Router>
2429
</div>
2530
</Provider>
2631
)

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: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React from 'react'
2-
import { Link, IndexLink } from 'react-router'
2+
import { NavLink as Link } 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 Routes from '../../routes'
67

78
const location = {
89
pathname : '/route/8080',
@@ -14,7 +15,7 @@ const location = {
1415
export default class PageLayout extends React.Component {
1516
/* 类的静态属性,不会被实例继承。ES试验特性写法:propTypes写入class */
1617
static propTypes = {
17-
children : PropTypes.node
18+
store : PropTypes.object
1819
}
1920

2021
constructor (props) {
@@ -36,20 +37,20 @@ export default class PageLayout extends React.Component {
3637
<div>
3738
<Navbar color='faded' light toggleable>
3839
<NavbarToggler right onClick={this.toggle} />
39-
<NavbarBrand to='/' tag={IndexLink}>Vortex React</NavbarBrand>
40+
<NavbarBrand to='/' tag={Link}>Vortex React</NavbarBrand>
4041
<Collapse isOpen={this.state.isOpen} navbar>
4142
<Nav className='ml-auto' navbar>
4243
<NavLink to='/counter' activeClassName='active' tag={Link}>Counter</NavLink>
4344
<NavLink to='/zen' activeClassName='active' tag={Link}>Zen</NavLink>
4445
<NavLink to='/elapse' activeClassName='active' tag={Link}>Elapse</NavLink>
4546
<NavLink to={location} activeClassName='active' tag={Link}>Route</NavLink>
46-
<NavLink to='/notFound' activeClassName='active' tag={Link}>404</NavLink>
47+
<NavLink to='/pageNotFound' activeClassName='active' tag={Link}>404</NavLink>
4748
<NavLink href='https://github.com/YutHelloWorld/vortex-react'>Github</NavLink>
4849
</Nav>
4950
</Collapse>
5051
</Navbar>
5152
<Container className='text-center page-layout__viewport'>
52-
{this.props.children}
53+
<Routes />
5354
</Container>
5455
</div>
5556
)

src/main.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@ import './styles/main.scss'
55

66
// Store Initialization
77
// ------------------------------------
8-
const store = createStore(window.__INITIAL_STATE__)
8+
export const store = createStore(window.__INITIAL_STATE__)
99

1010
// Render Setup
1111
// ------------------------------------
1212
const MOUNT_NODE = document.getElementById('root')
1313

1414
let render = () => {
1515
const App = require('./components/App').default
16-
const routes = require('./routes/index').default(store)
1716

1817
ReactDOM.render(
19-
<App store={store} routes={routes} />,
18+
<App store={store} />,
2019
MOUNT_NODE
2120
)
2221
}

src/routes/Counter/index.js

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

3-
export default (store) => ({
4-
path : 'counter',
5-
/* 动态路由 */
6-
getComponent (nextState, cb) {
7-
/* 代码分割 */
8-
require.ensure([], (require) => {
9-
const Counter = require('./containers/CounterContainer').default
10-
const reducer = require('./modules/counter').default
6+
injectReducer(store, { key : 'counter', reducer })
117

12-
/* 将counterReducer注入rootReducer */
13-
injectReducer(store, { key : 'counter', reducer })
14-
15-
cb(null, Counter)
16-
}, 'counter')
17-
}
18-
})
8+
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/Home/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import HomeView from './components/HomeView'
22

33
// Sync route definition
4-
export default {
5-
component : HomeView
6-
}
4+
export default HomeView

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

0 commit comments

Comments
 (0)