Skip to content

Commit e94c366

Browse files
committed
Update readme
1 parent a5df8b0 commit e94c366

1 file changed

Lines changed: 90 additions & 28 deletions

File tree

README.md

Lines changed: 90 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
# Trace Router
22

3-
The router for your app
3+
The next generation router for your app
44

55
### Installation
66

77
```
8-
yarn add trace-router
8+
yarn add effector trace-router
9+
```
10+
11+
### Exports
12+
13+
```ts
14+
export { createRouter } from './router';
915
```
1016

1117
### Examples
1218

13-
Create router:
19+
Create a router:
1420

1521
```js
1622
import history from 'history/browser';
@@ -47,12 +53,12 @@ export const info = router.merge([
4753
// Redirect from "/" to "/user"
4854
exactRoot.visible.watch((visible) => {
4955
if (visible) {
50-
router.redirect('/user');
56+
user.redirect();
5157
}
5258
});
5359
````
5460
55-
Use routes:
61+
Use routes in React (`trace-router-react`):
5662
5763
```jsx
5864
export const Root = () => (
@@ -84,39 +90,56 @@ export const InfoPage = () => (
8490
);
8591
````
8692

87-
Use links:
93+
You can also use `Route` component instead of a hook:
94+
95+
```tsx
96+
<Route of={map} component={MapPage} />
97+
```
98+
99+
Use links to navigate routes directly:
88100

89101
```jsx
90-
<Link to="/about" router={router}>About</Link>
102+
<Link to={about}>About</Link>
91103
````
92104
93-
Use can apply default router for Links:
105+
Use can add params to the route (if it has ones):
94106
95107
```jsx
96-
import history from 'history/browser';
97-
import { applyRouter, createRouter } from 'trace-router';
98-
export const router = createRouter({ history });
108+
<Link to={userTicket} params={{ id: 100 }}>Month</Link>
109+
````
110+
111+
The above link compiles to something like:
99112

100-
applyRouter(router);
113+
```jsx
114+
<a href="/user-tiket/100" onClick={/* prevent default & navigate */}>Join Us</a>
101115
````
102116
103-
And use Links without router:
117+
Here is how you compile route to a `string`:
104118
105119
```jsx
106-
<Link to="/join-us">Join Us</Link>
120+
const href = route.compile({
121+
params: { id: 100 },
122+
query: {
123+
lang: 'ru'
124+
},
125+
hash: '#description',
126+
})
107127
````
108128

109-
You can use `navigate` method:
129+
Manual route navigation:
110130

111131
```jsx
112-
export const Button = ({ to, children }) => (
113-
<button onClick={() => router.navigate(to)}>
114-
{children}
115-
</button>
116-
);
132+
<Button onClick={() => product.navigate({ id: '100' })} />
117133
````
118134
119-
There is also `replace`, `shift`, `back` and `forward` methods
135+
or `redirect` + `compile` as an example:
136+
137+
```jsx
138+
<Button onClick={() => product.router.redirect({
139+
to: product.compile({ params: { id: '100' } }),
140+
state: { back }
141+
})} />
142+
````
120143

121144
### Types
122145

@@ -126,8 +149,9 @@ There is also `replace`, `shift`, `back` and `forward` methods
126149
</summary>
127150

128151
```ts
152+
129153
export type Router<Q extends Query = Query, S extends State = State> = {
130-
history: MemoryHistory<S>;
154+
history: History<S>;
131155
historyUpdated: Event<Update<S>>;
132156
historyUpdate: Store<Update<S>>;
133157
navigate: Event<ToLocation<S>>;
@@ -148,7 +172,7 @@ export type Router<Q extends Query = Query, S extends State = State> = {
148172
noMatches: Store<boolean>;
149173
add: <P extends Params = Params>(
150174
pathConfig: Pattern | RouteConfig
151-
) => Route<P>;
175+
) => Route<P, Router<Q, S>>;
152176
merge: <T extends Route[]>(routes: T) => MergedRoute;
153177
none: <T extends Route[]>(routes: T) => MergedRoute;
154178
};
@@ -162,22 +186,60 @@ export type Router<Q extends Query = Query, S extends State = State> = {
162186
</summary>
163187

164188
```ts
165-
export type Route<P extends Params = Params> = {
189+
export type Route<P extends Params = Params, R = Router> = {
166190
visible: Store<boolean>;
167191
params: Store<null | P>;
168192
config: RouteConfig;
193+
compile: (compileConfig?: CompileConfig<P>) => string;
194+
router: R extends Router<infer Q, infer S> ? Router<Q, S> : never;
195+
navigate: Event<P | void>;
196+
redirect: Event<P | void>;
169197
};
170198
```
171199

172200
</details>
173201

174-
### Exports
202+
<details>
203+
204+
<summary>
205+
Other typings
206+
</summary>
175207

176208
```ts
177-
export { createRouter, applyRouter } from './router';
209+
export type ToLocation<S extends State = State> =
210+
| string
211+
| { to?: To; state?: S };
212+
export type Delta = number;
213+
export type Resource = string;
214+
export type Pattern = string;
215+
export interface Query extends ObjectString {}
216+
export interface Params extends ObjectUnknown {}
217+
218+
export type RouterConfig<S extends State> = {
219+
history?: History<S> | MemoryHistory<S>;
220+
};
178221
179-
export { useRoute, Route, Link } from './react';
180-
````
222+
export type RouteConfig = {
223+
path: Pattern;
224+
matchOptions?: ParseOptions & TokensToRegexpOptions & RegexpToFunctionOptions;
225+
};
226+
227+
export type CompileConfig<P extends Params = Params> = {
228+
params?: P;
229+
query?: string[][] | Record<string, string> | string | URLSearchParams;
230+
hash?: string;
231+
options?: ParseOptions & TokensToFunctionOptions;
232+
};
233+
234+
export type MergedRoute = {
235+
visible: Store<boolean>;
236+
routes: Route[];
237+
configs: RouteConfig[];
238+
};
239+
240+
```
241+
242+
</details>
181243

182244
### Docs
183245

0 commit comments

Comments
 (0)