forked from mirrorjs/mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
57 lines (46 loc) · 1.44 KB
/
router.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import React from 'react'
import PropTypes from 'prop-types'
import createBrowserHistory from 'history/createBrowserHistory'
import createHashHistory from 'history/createHashHistory'
import createMemoryHistory from 'history/createMemoryHistory'
import {ConnectedRouter, routerActions} from 'react-router-redux'
import {options} from './defaults'
import {dispatch} from './middleware'
import {actions} from './actions'
let history
export default function Router({history = getHistory(), children}) {
// Add `push`, `replace`, `go`, `goForward` and `goBack` methods to actions.routing,
// when called, will dispatch the crresponding action provided by react-router-redux.
actions.routing = Object.keys(routerActions).reduce((memo, action) => {
memo[action] = (...args) => {
dispatch(routerActions[action](...args))
}
return memo
}, {})
// ConnectedRouter will use the store from Provider automatically
return (
<ConnectedRouter history={history}>
{children}
</ConnectedRouter>
)
}
Router.propTypes = {
children: PropTypes.element.isRequired,
history: PropTypes.object
}
export function getHistory() {
if (history) {
return history
}
const {historyMode} = options
if (historyMode === 'browser') {
history = createBrowserHistory()
}
if (historyMode === 'hash') {
history = createHashHistory()
}
if (historyMode === 'memory') {
history = createMemoryHistory()
}
return history
}