Skip to content

Commit f9e196c

Browse files
committed
docs: add some reference for router
1 parent 113a7f3 commit f9e196c

File tree

2 files changed

+100
-12
lines changed

2 files changed

+100
-12
lines changed

docs/route.md

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,103 @@ Routing components allow you to create implement single page app routing functio
44

55
Contents:
66

7-
- `<RouteProvider>`
8-
- `<Route>`
9-
- `go()`
10-
- `withRoute()`
7+
- [`<Router>`](#router)
8+
- [`<Route>`](#route)
9+
- [`go()`](#go)
10+
- [`withRoute()`](#withroute)
1111

12-
## `<RouteProvider>`
1312

13+
## `<Router>`
14+
15+
`Router` is a root component that provides routing to your application. I should be placed above all other components
16+
that use routing. It uses React's context [`Provider`](./context.md#provider) component to provide route information to
17+
its children.
18+
19+
### Props
20+
21+
- `route` - optional, string, route to use for routing. If not specified, `<Router>` will use
22+
[`<LocationSensor>`](./LocationSensor.md) internally to track any changes to `window.location`.
23+
- `ns` - optional, string, namespaces of the router. This allows you to have many different routers
24+
on one page, each in a separate namespace.
25+
26+
Unlike other routing libraries `<Router>` component lets you specify the current route manually using the `route` property,
27+
this way you can use Redux or MobX, or any other state container library to store your route, if you want to.
1428

1529

1630
## `<Route>`
1731

32+
`Route` tries to match a fragment in a route. If it does match, the contents of the route is rendered. The contents of the route
33+
can be either regular JSX children or a FaCC or a React component specified in the `comp` prop.
34+
35+
36+
### Props
37+
38+
- `match`, optional, matching condition, defaults to `/.+/`, see discussion below.
39+
40+
The props object has the following TypeScript signature
41+
42+
```ts
43+
interface IRouteMatch {
44+
children?: React.ReactElement<any> | ((params) => React.ReactElement<any>);
45+
cnt?: number;
46+
comp?: React.ComponentClass<{}> | React.StatelessComponent<{}>;
47+
exact?: boolean;
48+
match?: TRouteMatcher | RegExp | string;
49+
ns?: string;
50+
preserve?: boolean;
51+
}
52+
```
53+
54+
55+
As you can see the `match` prop has the following signature
56+
57+
```ts
58+
TRouteMatcher | RegExp | string;
59+
```
60+
61+
where
62+
63+
```ts
64+
type TRouteMatcher = (route: string) => TRouteMatchResult;
65+
66+
interface TRouteMatchResult {
67+
length: number; // Length how many characters to truncate from route.
68+
matches?: RegExpMatchArray; // RegExp matches, if any.
69+
}
70+
```
71+
72+
- if `string`, it is converted to a regular expression with `^` prepended, which means it has to match the route starting from
73+
the very first character. For example, `/users` -> `/^(\/users)/`. If the `exact` prop is on, also `$` appended to the regular
74+
expression, which means the regular expression has to match the route exactly. For example, `/users` -> `/^(\/users)$`.
75+
- if `RegExp`, the specified regular expression will be used to match the current `route`, the resulting matches array will be
76+
returned, if any.
77+
- if `function` is provided, it will be treated as if it has type of `TRouteMatcher`, it is given a `route` string as a
78+
single argument. If it does not match the route, it has to return `null`. If it matches the `route`, it has to return an object
79+
with the following properties:
80+
- `length` - required, number of characters to truncate from the start of the route, for the inner routes, basically this should be
81+
equal to the length of the matched fragment of the path.
82+
- `matches` - optionsl, array of matches returned by `String.prototype.match()` function.
83+
84+
85+
## Example
86+
87+
### With Redux
88+
89+
```jsx
90+
import {Router, Route} from 'libreact/lib/route';
91+
import {connect} from 'react-redux';
92+
93+
const App = ({route}) =>
94+
<Router route={route}>
95+
<div>
96+
<Route match='/home' comp={Home} />
97+
<Route match='/users' comp={Users} />
98+
</div>
99+
</Router>;
100+
101+
const mapStateToProps = ({route}) => ({
102+
route
103+
});
18104

105+
export default connect(mapStateToProps)(App);
106+
```

src/route/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import {Component} from 'react';
22
import {LocationSensor} from '../LocationSensor';
33
import {Provider, Consumer} from '../context';
4-
import {h, sym} from '../util';
5-
6-
const $$location = sym('location');
4+
import {h, ns} from '../util';
75

86
export interface IRouteProviderProps {
97
children?: any;
8+
ns?: string;
109
route?: string;
1110
parent?: TRouteMatchResult;
1211
}
@@ -22,7 +21,7 @@ export class Router extends Component<IRouteProviderProps, any> {
2221
this.matches = 0;
2322

2423
const element = h(Provider, {
25-
name: $$location,
24+
name: ns(`route/${this.props.ns}`),
2625
value: {
2726
route,
2827
onMatch: this.onMatch,
@@ -55,16 +54,17 @@ export type TRouteMatcher = (route: string) => TRouteMatchResult;
5554

5655
export interface IRouteMatch {
5756
children?: React.ReactElement<any> | ((params) => React.ReactElement<any>);
57+
cnt?: number;
5858
comp?: React.ComponentClass<{}> | React.StatelessComponent<{}>;
5959
exact?: boolean;
6060
match?: TRouteMatcher | RegExp | string;
61-
cnt?: number;
61+
ns?: string;
6262
preserve?: boolean;
6363
}
6464

6565
export class Route extends Component<IRouteMatch, any> {
6666
static defaultProps = {
67-
match: /.*/,
67+
match: /.+/,
6868
cnt: 0
6969
};
7070

@@ -99,7 +99,7 @@ export class Route extends Component<IRouteMatch, any> {
9999
}
100100

101101
render () {
102-
return h(Consumer, {name: $$location}, ({route, onMatch, getMathces, parent}) => {
102+
return h(Consumer, {name: ns(`route/${this.props.ns}`)}, ({route, onMatch, getMathces, parent}) => {
103103
const {children, match} = this.props;
104104

105105
if (getMathces() <= this.props.cnt) {

0 commit comments

Comments
 (0)