@@ -10,8 +10,9 @@ export interface Props {
1010 [ propName : string ] : any ;
1111}
1212export interface State {
13- location : any ,
14- Component : React . ComponentClass < Props > ;
13+ location ?: any ,
14+ Component ?: React . ComponentClass < Props > ,
15+ props ?: any ;
1516}
1617export interface Action {
1718 ( ) : React . ReactElement < Props > ;
@@ -35,22 +36,27 @@ export default class Router extends React.Component<Props, State> {
3536 super ( ) ;
3637 this . state = {
3738 Component : props . Component ,
38- location : props . history . location
39+ location : props . history . location ,
40+ props : props . props
3941 } ;
4042
4143 this . router = props . router ;
4244 this . history = props . history ;
4345 }
44- static async init ( { path, routes, hooks } ) {
46+ static async init ( { path, routes, hooks, ctx = { } } ) {
4547 const plainRoutes = Router . buildRoutes ( routes ) ;
4648 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+ } ;
4853 return {
4954 Router,
5055 Component : result ,
5156 redirect,
5257 status,
5358 router,
59+ props,
5460 callback : this . makeCallback ( router )
5561 }
5662 }
@@ -97,9 +103,9 @@ export default class Router extends React.Component<Props, State> {
97103 get location ( ) {
98104 return this . state . location ;
99105 }
100- async navigate ( path ) {
106+ async navigate ( path , ctx = { } ) {
101107 try {
102- const { redirect } = await this . router . match ( { path, ctx : { } } ) ;
108+ const { redirect } = await this . router . match ( { path, ctx } ) ;
103109 if ( redirect ) {
104110 this . history . push ( redirect ) ;
105111 } else {
@@ -109,17 +115,30 @@ export default class Router extends React.Component<Props, State> {
109115 if ( this . props . errorHandler ) {
110116 this . props . errorHandler ( error , this ) ;
111117 } else {
112- console . error ( 'Navigate Error' , error ) ;
118+ console . error ( 'Match Error' , path , error ) ;
113119 throw error ;
114120 }
115121 }
116122 }
117123 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+ }
123142 } ;
124143 componentDidMount ( ) {
125144 this . unlistenHistroy = this . history . listen ( this . _locationChanged )
@@ -128,6 +147,6 @@ export default class Router extends React.Component<Props, State> {
128147 this . unlistenHistroy ( ) ;
129148 }
130149 render ( ) {
131- return < this . state . Component />
150+ return < this . state . Component router = { this . state . props } />
132151 }
133152}
0 commit comments