@@ -10,8 +10,9 @@ export interface Props {
10
10
[ propName : string ] : any ;
11
11
}
12
12
export interface State {
13
- location : any ,
14
- Component : React . ComponentClass < Props > ;
13
+ location ?: any ,
14
+ Component ?: React . ComponentClass < Props > ,
15
+ props ?: any ;
15
16
}
16
17
export interface Action {
17
18
( ) : React . ReactElement < Props > ;
@@ -35,22 +36,27 @@ export default class Router extends React.Component<Props, State> {
35
36
super ( ) ;
36
37
this . state = {
37
38
Component : props . Component ,
38
- location : props . history . location
39
+ location : props . history . location ,
40
+ props : props . props
39
41
} ;
40
42
41
43
this . router = props . router ;
42
44
this . history = props . history ;
43
45
}
44
- static async init ( { path, routes, hooks } ) {
46
+ static async init ( { path, routes, hooks, ctx = { } } ) {
45
47
const plainRoutes = Router . buildRoutes ( routes ) ;
46
48
const router = new RouterAsync ( { routes : plainRoutes , hooks } ) ;
47
- const { result, redirect, status } = await router . resolve ( { path } ) ;
49
+ const { result, redirect, status } = await router . resolve ( { path, ctx } ) ;
50
+ let props = {
51
+ ctx
52
+ } ;
48
53
return {
49
54
Router,
50
55
Component : result ,
51
56
redirect,
52
57
status,
53
58
router,
59
+ props,
54
60
callback : this . makeCallback ( router )
55
61
}
56
62
}
@@ -97,9 +103,9 @@ export default class Router extends React.Component<Props, State> {
97
103
get location ( ) {
98
104
return this . state . location ;
99
105
}
100
- async navigate ( path ) {
106
+ async navigate ( path , ctx = { } ) {
101
107
try {
102
- const { redirect } = await this . router . match ( { path, ctx : { } } ) ;
108
+ const { redirect } = await this . router . match ( { path, ctx } ) ;
103
109
if ( redirect ) {
104
110
this . history . push ( redirect ) ;
105
111
} else {
@@ -109,17 +115,30 @@ export default class Router extends React.Component<Props, State> {
109
115
if ( this . props . errorHandler ) {
110
116
this . props . errorHandler ( error , this ) ;
111
117
} else {
112
- console . error ( 'Navigate Error' , error ) ;
118
+ console . error ( 'Match Error' , path , error ) ;
113
119
throw error ;
114
120
}
115
121
}
116
122
}
117
123
private _locationChanged = async ( location , action ) => {
118
- const { result } = await this . router . resolve ( { path : location . pathname , ctx : { } } ) ;
119
- this . setState ( {
120
- Component : result ,
121
- location
122
- } , Router . makeCallback ( this . router ) ) ;
124
+ try {
125
+ const { result, ctx } = await this . router . resolve ( { path : location . pathname } ) ;
126
+ let props = {
127
+ ctx
128
+ } ;
129
+ this . setState ( {
130
+ Component : result ,
131
+ location,
132
+ props
133
+ } , Router . makeCallback ( this . router ) ) ;
134
+ } catch ( error ) {
135
+ if ( this . props . errorHandler ) {
136
+ this . props . errorHandler ( error , this ) ;
137
+ } else {
138
+ console . error ( 'Resolve Error' , location , error ) ;
139
+ throw error ;
140
+ }
141
+ }
123
142
} ;
124
143
componentDidMount ( ) {
125
144
this . unlistenHistroy = this . history . listen ( this . _locationChanged )
@@ -128,6 +147,6 @@ export default class Router extends React.Component<Props, State> {
128
147
this . unlistenHistroy ( ) ;
129
148
}
130
149
render ( ) {
131
- return < this . state . Component />
150
+ return < this . state . Component router = { this . state . props } />
132
151
}
133
152
}
0 commit comments