-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.tsx
127 lines (117 loc) · 2.58 KB
/
index.tsx
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import * as React from 'react'
import * as ReactDOM from 'react-dom'
import { Router, browserHistory, RouteConfig } from 'react-router'
import { History } from "history";
import { Map } from 'immutable'
import { Provider } from 'react-redux'
import { syncHistoryWithStore } from 'react-router-redux'
import configureStore, { Store } from './utils/configureStore'
export { Store }
export type Routes = (store: Store<any>) => RouteConfig
export interface options {
/**
* Can be a function with store parameter or a RouteConfig route
*
* @type {(Routes | RouteConfig)}
* @memberOf options
*/
routes: Routes | RouteConfig
/**
* Can be Map or any immutable type
*
*
* @memberOf options
*/
initialState?
/**
* Can be an object within {key:Function}
*
*
* @memberOf options
*/
asyncReducers?: {}
history?: History
rootReducer?: Function
/**
* custom render,you can add other Provider like react-intl .
*
* @type {Function}
* @memberOf options
*/
render?: Function
middlewares?
/**
* can use with hake-redux https://github.com/bang88/hake-redux#api
*/
client?: any
}
export interface Render {
store
routes: RouteConfig
history: History
}
/**
* default render
*/
const defaultRender = ({ store, routes, history }: Render) => <Provider store={store}>
<Router history={history}>
{routes}
</Router>
</Provider>
/**
* configure routes and others then start the app immediately
* @param {options} options
*/
const hake = ({
initialState = Map(),
asyncReducers = {},
history = browserHistory,
render = defaultRender,
routes,
rootReducer,
middlewares,
client,
}: options) => {
/**
* configure store
*/
const store = configureStore({ initialState, asyncReducers, rootReducer, middlewares, client })
/**
* sync history with immutable support
*/
const appHistory = syncHistoryWithStore(history, store, {
selectLocationState(state: any) {
return state.get('routing').toJS()
}
})
/**
* call routes with param store if it's a function
*/
if (typeof routes === 'function') {
routes = routes(store)
}
/**
* render params
*/
const renderParams = { store, routes, history: appHistory }
/**
* render
*/
const appRender = render(renderParams)
/**
* export store
*/
return {
store,
/**
* start the app
* @param {string} target The target app render to
*/
start: (target = 'root') =>
/**
* here we go
*/
ReactDOM.render(appRender, document.getElementById(target))
}
}
export default hake