Skip to content

Commit 179de08

Browse files
committed
add placeholder
1 parent 1af4094 commit 179de08

File tree

3 files changed

+64
-12
lines changed

3 files changed

+64
-12
lines changed

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Link from './link';
44
import RootRoute from './root-route';
55
import Middleware from './middleware';
66
import Redirect from './redirect';
7+
import PlaceHolder from './placeholder';
78
import { RouterError, Context } from 'router-async';
89

9-
export { Router, Route, Link, RootRoute, Middleware, Redirect, RouterError, Context };
10+
export { Router, Route, Link, RootRoute, Middleware, Redirect, PlaceHolder, RouterError, Context };

src/placeholder.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import * as React from 'react';
2+
import Router from './router';
3+
4+
export interface Props {
5+
[propName: string]: any;
6+
}
7+
export interface State {
8+
Component?: React.ComponentClass<Props>,
9+
props?: any;
10+
}
11+
export interface Context {
12+
router: Router
13+
}
14+
export default class PlaceHolder extends React.Component<Props, State> {
15+
context: Context;
16+
static contextTypes = {
17+
router: React.PropTypes.object.isRequired
18+
};
19+
constructor(props) {
20+
super();
21+
this.state = {
22+
Component: props.Component,
23+
props: props.props
24+
};
25+
}
26+
componentDidMount() {
27+
this.context.router.subscribe((Component, props, renderCallback) => {
28+
this.setState({
29+
Component,
30+
props
31+
}, renderCallback);
32+
})
33+
}
34+
render() {
35+
return <this.state.Component router={this.state.props} />
36+
}
37+
}

src/router.tsx

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export interface Props {
1010
[propName: string]: any;
1111
}
1212
export interface State {
13-
location?: any,
1413
Component?: React.ComponentClass<Props>,
1514
props?: any;
1615
}
@@ -32,16 +31,19 @@ export default class Router extends React.Component<Props, State> {
3231
private router: any;
3332
private history: any;
3433
private unlistenHistroy: any;
34+
private subscriber: any;
35+
private location: any;
3536
constructor(props) {
3637
super();
3738
this.state = {
3839
Component: props.Component,
39-
location: props.history.location,
4040
props: props.props
4141
};
4242

43+
this.location = props.history.location;
4344
this.router = props.router;
4445
this.history = props.history;
46+
this.subscriber = null;
4547
}
4648
static async init({ path, routes, hooks, silent = false, ctx = new Context() }) {
4749
const plainRoutes = Router.buildRoutes(routes);
@@ -93,9 +95,6 @@ export default class Router extends React.Component<Props, State> {
9395
router: this
9496
};
9597
}
96-
get location() {
97-
return this.state.location;
98-
}
9998
async navigate(path, ctx = new Context()) {
10099
try {
101100
const { redirect } = await this.router.match({ path, ctx });
@@ -112,6 +111,19 @@ export default class Router extends React.Component<Props, State> {
112111
}
113112
}
114113
}
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+
}
115127
private _locationChanged = async (location, action) => {
116128
try {
117129
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> {
123135
redirect,
124136
ctx
125137
};
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);
131141
} catch (error) {
132142
if (this.props.errorHandler) {
133143
this.props.errorHandler(error, this);
@@ -144,6 +154,10 @@ export default class Router extends React.Component<Props, State> {
144154
this.unlistenHistroy();
145155
}
146156
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+
}
148162
}
149163
}

0 commit comments

Comments
 (0)