@@ -10,7 +10,6 @@ export interface Props {
10
10
[ propName : string ] : any ;
11
11
}
12
12
export interface State {
13
- location ?: any ,
14
13
Component ?: React . ComponentClass < Props > ,
15
14
props ?: any ;
16
15
}
@@ -32,16 +31,19 @@ export default class Router extends React.Component<Props, State> {
32
31
private router : any ;
33
32
private history : any ;
34
33
private unlistenHistroy : any ;
34
+ private subscriber : any ;
35
+ private location : any ;
35
36
constructor ( props ) {
36
37
super ( ) ;
37
38
this . state = {
38
39
Component : props . Component ,
39
- location : props . history . location ,
40
40
props : props . props
41
41
} ;
42
42
43
+ this . location = props . history . location ;
43
44
this . router = props . router ;
44
45
this . history = props . history ;
46
+ this . subscriber = null ;
45
47
}
46
48
static async init ( { path, routes, hooks, silent = false , ctx = new Context ( ) } ) {
47
49
const plainRoutes = Router . buildRoutes ( routes ) ;
@@ -93,9 +95,6 @@ export default class Router extends React.Component<Props, State> {
93
95
router : this
94
96
} ;
95
97
}
96
- get location ( ) {
97
- return this . state . location ;
98
- }
99
98
async navigate ( path , ctx = new Context ( ) ) {
100
99
try {
101
100
const { redirect } = await this . router . match ( { path, ctx } ) ;
@@ -112,6 +111,19 @@ export default class Router extends React.Component<Props, State> {
112
111
}
113
112
}
114
113
}
114
+ subscribe ( callback : Function ) {
115
+ this . subscriber = callback ;
116
+ }
117
+ changeComponent ( Component , props , renderCallback ) {
118
+ if ( this . subscriber ) {
119
+ this . subscriber ( Component , props , renderCallback ) ;
120
+ } else {
121
+ this . setState ( {
122
+ Component,
123
+ props
124
+ } , renderCallback ) ;
125
+ }
126
+ }
115
127
private _locationChanged = async ( location , action ) => {
116
128
try {
117
129
const { path, route, status, params, redirect, result, ctx } = await this . router . run ( { path : location . pathname } ) ;
@@ -123,11 +135,9 @@ export default class Router extends React.Component<Props, State> {
123
135
redirect,
124
136
ctx
125
137
} ;
126
- this . setState ( {
127
- Component : result ,
128
- location,
129
- props
130
- } , Router . makeCallback ( this . router , { path, route, status, params, redirect, result, ctx } ) ) ;
138
+ this . location = location ;
139
+ let renderCallback = Router . makeCallback ( this . router , { path, route, status, params, redirect, result, ctx } ) ;
140
+ this . changeComponent ( result , props , renderCallback ) ;
131
141
} catch ( error ) {
132
142
if ( this . props . errorHandler ) {
133
143
this . props . errorHandler ( error , this ) ;
@@ -144,6 +154,10 @@ export default class Router extends React.Component<Props, State> {
144
154
this . unlistenHistroy ( ) ;
145
155
}
146
156
render ( ) {
147
- return < this . state . Component router = { this . state . props } />
157
+ if ( this . props . children ) {
158
+ return React . Children . only ( this . props . children )
159
+ } else {
160
+ return < this . state . Component router = { this . state . props } />
161
+ }
148
162
}
149
163
}
0 commit comments