Skip to content

Commit 52a1fde

Browse files
committed
docs: improve docs, add example from react-router
1 parent c9f38c9 commit 52a1fde

File tree

5 files changed

+91
-19
lines changed

5 files changed

+91
-19
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# libreact
22

3-
React standard library.
3+
React standard library, must-have toolbox for any React project.
44

5-
- Collection of React goodies necessary for every project.
5+
- Collection of React sensors, FaCCs, HOCs, context, lazy loading, and [other goodies](#contents).
66
- Isomorphic - all components work in browser and Node.js (and some in `react-native`).
7-
- [See demos and docs](https://mailonline.github.io/mol-fe-react/).
7+
- [See demos and docs](https://mailonline.github.io/libreact/).
88

99
## Installation
1010

@@ -22,7 +22,7 @@ import {mock} from 'libreact/lib/mock';
2222
const MyComponent = mock();
2323
```
2424

25-
## Reference
25+
## Contents
2626

2727
- Dummies
2828
- [`mock()`](./docs/mock.md)

docs/route.md

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Route
22

3-
Routing components allow you to create implement single page app routing functionality.
3+
Routing components that allow you to implement single page app routing functionality.
44

5-
- Provides *dynamic* routing, [just like `react-router`](https://reacttraining.com/react-router/core/guides/philosophy/dynamic-routing)
6-
- Use any state container
7-
- Multiple routers on one page
5+
- Provide *dynamic* routing, [just like `react-router`](https://reacttraining.com/react-router/core/guides/philosophy/dynamic-routing)
6+
- Use [any state container](#use-any-state-container)
7+
- [Multiple routers](#multiple-routers) on one page
88

9-
Contents:
9+
Reference:
1010

1111
- [`<Router>`](#router)
1212
- [`<Route>`](#route)
@@ -17,24 +17,25 @@ Contents:
1717
## Use any state container
1818

1919
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.
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.
2121

22-
The "problem-of-routers" is that they all want to be the ones that keep the state of the route. For example, [`react-router`](https://reacttraining.com/react-router/) uses
22+
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
2323
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/).
2424

2525
So, if you wanted to store the state of the route in Redux, there was no good way for you to do that using `react-router`, that is why
2626
[`redux-little-router`](https://github.com/FormidableLabs/redux-little-router) was born, however, `redux-little-router` itself hoards the
2727
state of the route in Redux, so you cannot use it if you use any other state container, say MobX.
2828

29-
Libreact is completely orthogonal to where you store the current route, all you have to do is provide the current route to the `<Router>` component:
29+
Libreact is completely orthogonal to where you store the *current route*, all you have to do is provide the current route to the `<Router>`
30+
coponent using the `route` prop.
3031

3132
```jsx
3233
<Router route={currentRoute}>
3334
{/* ... */}
3435
</Router>
3536
```
3637

37-
You can story in Redux or MobX, or really anywhere you want. And if you don't want to bother, don't! Just use the current location of the browser:
38+
You can store it in Redux or MobX, or really anywhere you want. And if you don't want to bother, don't! Just use the current location of the browser:
3839

3940
```jsx
4041
<LocationSensor>{({pathname}) =>
@@ -44,7 +45,7 @@ You can story in Redux or MobX, or really anywhere you want. And if you don't wa
4445
}</LocationSensor>
4546
```
4647

47-
Actually, you don't even have to do that, if you don't explicitly specify the `route` prop, the `<Router>` component will do exactly that for you under the hood,
48+
Actually, you don't even have to do that, if you don't explicitly specify the `route` prop, the `<Router>` component will do exactly that for you under-the-hood,
4849
so, simply use:
4950

5051
```jsx
@@ -54,13 +55,25 @@ so, simply use:
5455
```
5556

5657

57-
## `<Router>`
58+
## Multiple routers
59+
60+
You can have many routers operating on the same page in parallel. All you have to do is specify a *namespace* using the `ns` props.
61+
62+
```jsx
63+
<Router ns='secret'>
64+
<Route ns='secret' />
65+
</Router>
66+
```
67+
68+
## Reference
69+
70+
### `<Router>`
5871

5972
`Router` is a root component that provides routing to your application. I should be placed above all other components
6073
that use routing. It uses React's context [`Provider`](./context.md#provider) component to provide route information to
6174
its children.
6275

63-
### Props
76+
#### Props
6477

6578
- `route` - optional, string, route to use for routing. If not specified, `<Router>` will use
6679
[`<LocationSensor>`](./LocationSensor.md) internally to track any changes to `window.location`.
@@ -71,13 +84,13 @@ Unlike other routing libraries `<Router>` component lets you specify the current
7184
this way you can use Redux or MobX, or any other state container library to store your route, if you want to.
7285

7386

74-
## `<Route>`
87+
### `<Route>`
7588

7689
`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
7790
can be either regular JSX children or a FaCC or a React component specified in the `comp` prop.
7891

7992

80-
### Props
93+
#### Props
8194

8295
- `match`, optional, matching condition, defaults to `/.+/`, see discussion below.
8396

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import {h} from '../../util';
2+
import {Router, Route} from '..';
3+
4+
const StoryRouteExample2 = () => (
5+
<Router>
6+
<div>
7+
<ul>
8+
<li><a onClick={() => history.pushState(null, '', '/')}>Home</a></li>
9+
<li><a onClick={() => history.pushState(null, '', '/about')}>About</a></li>
10+
<li><a onClick={() => history.pushState(null, '', '/topics')}>Topics</a></li>
11+
</ul>
12+
13+
<hr/>
14+
15+
<Route exact match='/' comp={Home} />
16+
<Route match='/about' comp={About} />
17+
<Route match='/topics' comp={Topics} />
18+
</div>
19+
</Router>
20+
);
21+
22+
const Home = () => (
23+
<div>
24+
<h2>Home</h2>
25+
</div>
26+
);
27+
28+
const About = () => (
29+
<div>
30+
<h2>About</h2>
31+
</div>
32+
);
33+
34+
const Topics = ({ match }) => (
35+
<div>
36+
<h2>Topics</h2>
37+
<ul>
38+
<li>
39+
Rendering with React
40+
</li>
41+
<li>
42+
Components
43+
</li>
44+
<li>
45+
Props v. State
46+
</li>
47+
</ul>
48+
</div>
49+
);
50+
51+
const Topic = ({ match }) => (
52+
<div>
53+
<h3>{match.params.topicId}</h3>
54+
</div>
55+
);
56+
57+
export default StoryRouteExample2;

src/route/__story__/story.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import {LocationSensor} from '../../LocationSensor';
66
import {Router, Route} from '..';
77
import ShowDocs from '../../../.storybook/ShowDocs'
88
import StoryRouteExample from './StoryRouteExample';
9+
import StoryRouteExample2 from './StoryRouteExample2';
910
import StoryRouteTruncateRoute from './StoryRouteTruncateRoute';
1011
import StoryRoutePreserveRoute from './StoryRoutePreserveRoute';
1112

1213
storiesOf('Context/route', module)
1314
.add('Documentation', () => h(ShowDocs, {name: 'route'}))
1415
.add('Example', () => h(StoryRouteExample))
16+
.add('Example 2', () => h(StoryRouteExample2))
1517
.add('Truncates route', () => h(StoryRouteTruncateRoute))
1618
.add('Preserve route', () => h(StoryRoutePreserveRoute));

src/route/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export type TRouteMatcher = (route: string) => TRouteMatchResult;
5656
export interface IRouteMatch {
5757
children?: React.ReactElement<any> | ((params) => React.ReactElement<any>);
5858
cnt?: number;
59-
comp?: React.ComponentClass<{}> | React.StatelessComponent<{}>;
59+
comp?: React.ComponentClass<any> | React.StatelessComponent<any>;
6060
exact?: boolean;
6161
match?: TRouteMatcher | RegExp | string;
6262
ns?: string;

0 commit comments

Comments
 (0)