Skip to content

Commit 722538b

Browse files
committed
refactor
1 parent 9b36fd1 commit 722538b

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

src/index.ts

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

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

src/middleware.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default class Middleware extends React.Component<Props, State> {
2020
action: React.PropTypes.func
2121
};
2222
render() {
23-
return React.Children.only(this.props.children);
23+
const childs = Array.isArray(this.props.children ? this.props.children : [this.props.children]);
24+
return React.Children.only(childs);
2425
}
2526
}

src/router.tsx

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import RouterAsync from 'router-async';
1+
import { Router as RouterAsync, Context } from 'router-async';
22
import * as React from 'react';
33
import { deepMap } from './helpers';
44

@@ -43,11 +43,16 @@ export default class Router extends React.Component<Props, State> {
4343
this.router = props.router;
4444
this.history = props.history;
4545
}
46-
static async init({ path, routes, hooks, ctx = {} }) {
46+
static async init({ path, routes, hooks, ctx = new Context() }) {
4747
const plainRoutes = Router.buildRoutes(routes);
4848
const router = new RouterAsync({ routes: plainRoutes, hooks });
49-
const { result, redirect, status } = await router.resolve({ path, ctx });
49+
const { route, status, params, redirect, result } = await router.resolve({ path, ctx });
5050
let props = {
51+
path,
52+
route,
53+
status,
54+
params,
55+
redirect,
5156
ctx
5257
};
5358
return {
@@ -57,7 +62,7 @@ export default class Router extends React.Component<Props, State> {
5762
status,
5863
router,
5964
props,
60-
callback: this.makeCallback(router)
65+
callback: this.makeCallback(router, { path, route, status, params, redirect, result, ctx })
6166
}
6267
}
6368
static buildRoutes(routes) {
@@ -103,7 +108,7 @@ export default class Router extends React.Component<Props, State> {
103108
get location() {
104109
return this.state.location;
105110
}
106-
async navigate(path, ctx = {}) {
111+
async navigate(path, ctx = new Context()) {
107112
try {
108113
const { redirect } = await this.router.match({ path, ctx });
109114
if (redirect) {
@@ -112,25 +117,29 @@ export default class Router extends React.Component<Props, State> {
112117
this.history.push(path);
113118
}
114119
} catch (error) {
115-
if (this.props.errorHandler) {
116-
this.props.errorHandler(error, this);
117-
} else {
120+
this.history.push(path);
121+
if (!this.props.errorHandler) {
118122
console.error('Match Error', path, error);
119123
throw error;
120124
}
121125
}
122126
}
123127
private _locationChanged = async (location, action) => {
124128
try {
125-
const { result, ctx } = await this.router.resolve({ path: location.pathname });
129+
const { path, route, status, params, redirect, result, ctx } = await this.router.resolve({ path: location.pathname });
126130
let props = {
131+
path,
132+
route,
133+
status,
134+
params,
135+
redirect,
127136
ctx
128137
};
129138
this.setState({
130139
Component: result,
131140
location,
132141
props
133-
}, Router.makeCallback(this.router));
142+
}, Router.makeCallback(this.router, { path, route, status, params, redirect, result, ctx }));
134143
} catch (error) {
135144
if (this.props.errorHandler) {
136145
this.props.errorHandler(error, this);

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ react@^15.3.2:
9090
loose-envify "^1.1.0"
9191
object-assign "^4.1.0"
9292

93-
router-async@0.0.1:
94-
version "0.0.1"
95-
resolved "https://registry.yarnpkg.com/router-async/-/router-async-0.0.1.tgz#884b5bcbdffbd23bf4a202ab6a99b3a0bb37bcde"
93+
router-async:
94+
version "0.0.4"
95+
resolved "https://registry.yarnpkg.com/router-async/-/router-async-0.0.4.tgz#290575273b9ee4206ed6b587221d3a23d5116ef1"
9696
dependencies:
9797
path-to-regexp "^1.6.0"
9898

0 commit comments

Comments
 (0)