@@ -4,15 +4,103 @@ Routing components allow you to create implement single page app routing functio
44
55Contents:
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+ ```
0 commit comments