Skip to content

Commit fd84744

Browse files
committed
docs: ✏️ update router docs
1 parent fc72e6e commit fd84744

File tree

5 files changed

+61
-79
lines changed

5 files changed

+61
-79
lines changed

README.md

+3-7
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const MyComponent = mock();
7474
- [`<Provider>`](./docs/en/Provider.md#provider), [`<Consumer>`](./docs/en/Provider.md#consumer), [`withContext()`](./docs/en/Provider.md#withcontext-hoc), and [`@withContext`](./docs/en/Provider.md#withcontext-decorator)
7575
- [`<Theme>`](./docs/en/theme.md#theme), [`<Themed>`](./docs/en/theme.md#themed), [`withTheme()`](./docs/en/theme.md#withtheme-hoc), and [`@withTheme`](./docs/en/theme.md#withtheme-decorator)
7676
- [`<CssVarsProvider>`](./docs/en/cssvars.md), [`<CssVars>`](./docs/en/cssvars.md#cssvars), [`withCssVars()`](./docs/en/cssvars.md#withcssvars-hoc), and [`@withCssVars`](./docs/en/cssvars.md#withcssvars-decorator)
77-
- [`<Router>`](./docs/en/routing.md#router), [`<Route>`](./docs/en/routing.md#route), [`withRoute()`](./docs/en/routing.md#withroute), `@withRoute`, `go()`, and `<Go>`
77+
- [Router](./docs/en/routing.md)
7878
- [`<Translations>`](./docs/en/translate.md#translations), [`<Translate>`](./docs/en/translate.md#translate-or-t), [`<T>`](./docs/en/translate.md#translate-or-t), [`withT()`](./docs/en/translate.md#witht-hoc), and [`@withT`](./docs/en/translate.md#witht-decorator)
7979
- [UI](./docs/en/UI.md)
8080
- [`<Portal>`](./docs/en/Portal.md), [`<Overlay>`](./docs/en/Overlay.md), and [`<Modal>`](./docs/en/Modal.md)
@@ -100,20 +100,16 @@ const MyComponent = mock();
100100
- [`<LocalStorage>`](./docs/en/LocalStorage.md), `<SessionStorage>`, `<IndexedDb>`
101101
- [`<ClassNames>`](./docs/en/ClassNames.md)
102102
- `<Title>`, `<Favicon>`
103-
- [`go()`](./docs/en/routing.md#go), `<Redirect>`, `<Link>`, [`<Sms>`](./docs/en/Sms.md), [`<Mailto>`](./docs/en/Mailto.md), and `<Tel>`
103+
- [`go()`](./docs/en/routing.md#go), [`<Sms>`](./docs/en/Sms.md), [`<Mailto>`](./docs/en/Mailto.md), and `<Tel>`
104104
- [Boundaries](./docs/en/Boundaries.md)
105105
- [`<BrowserOnly>`](./docs/en/BrowserOnly.md), [`<ServerOnly>`](./docs/en/ServerOnly.md), and [`<ElectronOnly>`](./docs/en/ElectronOnly.md)
106106
- [`<ErrorBoundary>`](./docs/en/ErrorBoundary.md) and [`withErrorBoundary()`](./docs/en/ErrorBoundary.md#witherrorboundary-hoc)
107107
- `<CacheBoundary>`
108108
- [NEXT](./docs/en/next.md)
109109
- [`createRef()`](./docs/en/next/createRef.md), [`createState()`](./docs/en/next/createState.md), and [`createLifecycleEvents()`](./docs/en/next/createLifecycleEvents.md)
110110
- Other
111-
- [`<Resolve>`](./docs/en/Resolve.md), `<Fetch>`
111+
- [`<Resolve>`](./docs/en/Resolve.md)
112112
- [`getDisplayName()`](./docs/en/getDisplayName.md)
113-
- `<Locales>`
114-
- `<Draggable>`, `<Droppable>`, `<Parallax>`, `<Pin>`
115-
- Electron
116-
- `<Menu>` and `<MenuItem>`
117113

118114

119115
## License

docs/en/README.md

+3-6
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
- [`<Provider>`](./Provider.md#provider), [`<Consumer>`](./Provider.md#consumer), [`withContext()`](./Provider.md#withcontext-hoc), and [`@withContext`](./Provider.md#withcontext-decorator)
4545
- [`<Theme>`](./theme.md#theme), [`<Themed>`](./theme.md#themed), [`withTheme()`](./theme.md#withtheme-hoc), and [`@withTheme`](./theme.md#withtheme-decorator)
4646
- [`<CssVarsProvider>`](./cssvars.md), [`<CssVars>`](./cssvars.md#cssvars), [`withCssVars()`](./cssvars.md#withcssvars-hoc), and [`@withCssVars`](./cssvars.md#withcssvars-decorator)
47-
- [`<Router>`](./routing.md#router), [`<Route>`](./routing.md#route), [`withRoute()`](./routing.md#withroute), `@withRoute`, `go()`, and `<Go>`
47+
- [Router](./routing.md)
4848
- [`<Translations>`](./translate.md#translations), [`<Translate>`](./translate.md#translate-or-t), [`<T>`](./translate.md#translate-or-t), [`withT()`](./translate.md#witht-hoc), and [`@withT`](./translate.md#witht-decorator)
4949
- [UI](./UI.md)
5050
- [`<Portal>`](./Portal.md), [`<Overlay>`](./Overlay.md), and [`<Modal>`](./Modal.md)
@@ -70,16 +70,13 @@
7070
- [`<LocalStorage>`](./LocalStorage.md), `<SessionStorage>`, `<IndexedDb>`
7171
- [`<ClassNames>`](./ClassNames.md)
7272
- `<Title>`, `<Favicon>`
73-
- [`go()`](./routing.md#go), `<Redirect>`, `<Link>`, [`<Sms>`](./Sms.md), [`<Mailto>`](./Mailto.md), and `<Tel>`
73+
- [`go()`](./routing.md#go), [`<Sms>`](./Sms.md), [`<Mailto>`](./Mailto.md), and `<Tel>`
7474
- [Boundaries](./Boundaries.md)
7575
- [`<BrowserOnly>`](./BrowserOnly.md), [`<ServerOnly>`](./ServerOnly.md), and [`<ElectronOnly>`](./ElectronOnly.md)
7676
- [`<ErrorBoundary>`](./ErrorBoundary.md) and [`withErrorBoundary()`](./ErrorBoundary.md#witherrorboundary-hoc)
7777
- `<CacheBoundary>`
7878
- [NEXT](./next.md)
7979
- [`createRef()`](./next/createRef.md), [`createState()`](./next/createState.md), and [`createLifecycleEvents()`](./next/createLifecycleEvents.md)
8080
- Other
81-
- [`<Resolve>`](./Resolve.md), `<Fetch>`
81+
- [`<Resolve>`](./Resolve.md)
8282
- [`getDisplayName()`](./getDisplayName.md)
83-
- `<BrowserOnly>`, `<ServerOnly>`, and `<Environment>`
84-
- `<Locales>`
85-
- `<Draggable>`, `<Droppable>`, `<Parallax>`, `<Pin>`

docs/en/routing.md

+31-66
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ Reference:
1010

1111
- [`<Router>`](#router)
1212
- [`<Route>`](#route)
13+
- [`<Switch>`](#switch)
14+
- [`<Match>`](#match)
15+
- [`<Link>`](#link)
1316
- [`go()`](#go)
14-
- [`withRoute()`](#withroute)
17+
- [`createRouter()`](#createrouter)
1518

1619

1720
## Use any state container
1821

1922
With libreact's `<Router>` you can choose to store the current route in your state continer (like Redux or MobX) of
20-
choice, or not bother about it at all, in which case the `<Router>` will just use the current browser location out-of-the-box.
23+
choice.
2124

2225
The "problem-of-all-routers" is that they all want to keep the state of the route. For example, [`react-router`](https://reacttraining.com/react-router/) uses
2326
its internal history objects to store route information, and [it does not give you good access to that data structure](http://formidable.com/blog/2016/07/11/let-the-url-do-the-talking-part-1-the-pain-of-react-router-in-redux/).
@@ -65,8 +68,7 @@ You can have many routers operating on the same page in parallel. All you have t
6568
</Router>
6669
```
6770

68-
This allows you to route by basically anything, not just the current page location. You can have *app-inside-app* that has its
69-
own routing logic.
71+
You can have *app-inside-app* that has its own routing logic.
7072

7173

7274
## Reference
@@ -79,68 +81,41 @@ its children.
7981

8082
#### Props
8183

82-
- `route` - optional, string, route to use for routing. If not specified, `<Router>` will use
83-
[`<LocationSensor>`](./LocationSensor.md) internally to track any changes to `window.location`.
84-
- `ns` - optional, string, namespaces of the router. This allows you to have many different routers
84+
- `route` &mdash; required, string, route to use for routing. If not specified, `<Router>` will use
85+
- `ns` &mdash; optional, string, namespaces of the router. This allows you to have many different routers
8586
on one page, each in a separate namespace.
8687

87-
Unlike other routing libraries `<Router>` component lets you specify the current route manually using the `route` property,
88-
this way you can use Redux or MobX, or any other state container library to store your route, if you want to.
88+
Unlike in other routing libraries `<Router>` component forces you specify the current route manually using the `route` property,
89+
this way you can use Redux or MobX, or any other state container library to store your route.
8990

9091

9192
### `<Route>`
9293

9394
`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
94-
can be either regular JSX children or a FaCC or a React component specified in the `comp` prop.
95+
can be either regular JSX children or a FaCC, render prop, or component prop.
9596

9697

9798
#### Props
9899

99-
- `match`, optional, matching condition, defaults to `/.+/`, see discussion below.
100+
- `match` &mdash; optional, matching condition, defaults to empty string. This can be a regular expression.
101+
- `exact` &mdash; optiona, boolean, whether string route has to match exactly.
102+
- `truncate` &mdash; optiona, boolean, whether matched part of the route should be truncated for the nested routes.
103+
- `ns` &mdash; optional, string, namespace of the router.
100104

101-
The props object has the following TypeScript signature
102105

103-
```ts
104-
interface IRouteMatch {
105-
children?: React.ReactElement<any> | ((params) => React.ReactElement<any>);
106-
cnt?: number;
107-
comp?: React.ComponentClass<{}> | React.StatelessComponent<{}>;
108-
exact?: boolean;
109-
match?: TRouteMatcher | RegExp | string;
110-
ns?: string;
111-
preserve?: boolean;
112-
}
113-
```
114-
115-
116-
As you can see the `match` prop has the following signature
117-
118-
```ts
119-
TRouteMatcher | RegExp | string;
120-
```
121-
122-
where
123-
124-
```ts
125-
type TRouteMatcher = (route: string) => TRouteMatchResult;
126-
127-
interface TRouteMatchResult {
128-
length: number; // Length how many characters to truncate from route.
129-
matches?: RegExpMatchArray; // RegExp matches, if any.
130-
}
131-
```
106+
`match` prop can be a string, `RegExp` of a `function.
132107

133108
- if `string`, it is converted to a regular expression with `^` prepended, which means it has to match the route starting from
134109
the very first character. For example, `/users` -> `/^(\/users)/`. If the `exact` prop is on, also `$` appended to the regular
135110
expression, which means the regular expression has to match the route exactly. For example, `/users` -> `/^(\/users)$`.
136111
- if `RegExp`, the specified regular expression will be used to match the current `route`, the resulting matches array will be
137112
returned, if any.
138-
- if `function` is provided, it will be treated as if it has type of `TRouteMatcher`, it is given a `route` string as a
139-
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
140-
with the following properties:
141-
- `length` - required, number of characters to truncate from the start of the route, for the inner routes, basically this should be
142-
equal to the length of the matched fragment of the path.
143-
- `matches` - optional, array of matches returned by `String.prototype.match()` function.
113+
- if `function` is provided, it has to return an array of strings on a match.
114+
115+
116+
### `<Match>`
117+
118+
`<Match>` component is similar to `<Route>`, but it always renders its children, regarles if route was actually matched.
144119

145120

146121
### `go()`
@@ -164,25 +139,15 @@ interface IGoParams {
164139
- `state` - any serializable JavaScript object to store with the current history state. Useful, for example, to store current scroll position.
165140

166141

167-
## Example
142+
### `<Link>`
168143

169-
### With Redux
144+
Renders a link that will change browser location on click.
170145

171-
```jsx
172-
import {Router, Route} from 'libreact/lib/route';
173-
import {connect} from 'react-redux';
174-
175-
const App = ({route}) =>
176-
<Router route={route}>
177-
<div>
178-
<Route match='/home' comp={Home} />
179-
<Route match='/users' comp={Users} />
180-
</div>
181-
</Router>;
182-
183-
const mapStateToProps = ({route}) => ({
184-
route
185-
});
186-
187-
export default connect(mapStateToProps)(App);
146+
147+
### `createRouter()`
148+
149+
Create a new collection of router components given a namespace.
150+
151+
```js
152+
const {Router, Route, Match} = createRouter('inner-router', go);
188153
```

src/route/createRouter.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {h} from '../util';
2+
import Router from './Router';
3+
import Match from './Match';
4+
import Route from './Route';
5+
import Switch from './Switch';
6+
import defaultGo from './go';
7+
import Link from './Link';
8+
9+
const createRouter = (ns: string, go = defaultGo) => {
10+
const proxy = (Comp) => (props) => h(Comp, {...props, ns});
11+
12+
return {
13+
Router: proxy(Router),
14+
Match: proxy(Match),
15+
Route: proxy(Route),
16+
Switch: proxy(Switch),
17+
go,
18+
Link: (props) => h(Link, {...props, go}),
19+
};
20+
};
21+
22+
export default createRouter;

src/route/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Route from './Route';
44
import Switch from './Switch';
55
import go from './go';
66
import Link from './Link';
7+
import createRouter from './createRouter';
78

89
export {
910
Router,
@@ -12,4 +13,5 @@ export {
1213
Switch,
1314
go,
1415
Link,
16+
createRouter,
1517
};

0 commit comments

Comments
 (0)