@tinkoff/router 0.1.60 → 0.1.64
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -41
- package/lib/index.browser.js +2 -2
- package/lib/index.d.ts +1 -0
- package/lib/index.es.js +2 -2
- package/lib/index.js +7 -1
- package/lib/types.d.ts +2 -2
- package/package.json +1 -1
- package/README.en.md +0 -233
package/README.md
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
# @tinkoff/router
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Routing library. It can work both on the server and on the client. Designed primarily for building isomorphic applications.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Installation
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
You need to install `@tinkoff/router`:
|
|
8
8
|
|
|
9
9
|
```bash
|
|
10
10
|
yarn add @tinkoff/router
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
And connect it to the project:
|
|
14
14
|
|
|
15
15
|
```tsx
|
|
16
16
|
import { Router } from '@tinkoff/router';
|
|
@@ -20,25 +20,25 @@ const router = new Router();
|
|
|
20
20
|
|
|
21
21
|
## Explanation
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
Features:
|
|
24
24
|
|
|
25
|
-
-
|
|
26
|
-
-
|
|
27
|
-
-
|
|
28
|
-
-
|
|
29
|
-
-
|
|
25
|
+
- The library supports options for working both on the server and on the client.
|
|
26
|
+
- It is possible to use different client transition options: with or without SPA transitions.
|
|
27
|
+
- There are Guards to check the availability of a route under specific conditions.
|
|
28
|
+
- You can subscribe to different stages of the transition through hooks
|
|
29
|
+
- Components and hooks for easy routing from react
|
|
30
30
|
|
|
31
|
-
###
|
|
31
|
+
### Server and client version
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
It is enough just to import routing from the library itself and, based on the settings in package.json, the required version for the server or client will be returned
|
|
34
34
|
|
|
35
35
|
```ts
|
|
36
36
|
import { Router } from '@tinkoff/router';
|
|
37
37
|
```
|
|
38
38
|
|
|
39
|
-
###
|
|
39
|
+
### Client routing with/without SPA transitions
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
By default, routing with SPA transitions is enabled on the client. If you need to disable SPA transitions, you need to import a special version of the routing
|
|
42
42
|
|
|
43
43
|
```ts
|
|
44
44
|
import { Router, SpaHistory } from '@tinkoff/router';
|
|
@@ -50,27 +50,27 @@ const noSpaRouter = new NoSpaRouter();
|
|
|
50
50
|
|
|
51
51
|
### Router Guards
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
Guards allow you to control the availability of a particular route for a specific transition. From the guard, you can block the transition or initiate a redirect.
|
|
54
54
|
|
|
55
55
|
```ts
|
|
56
56
|
import { NavigationGuard } from '@tinkoff/router';
|
|
57
57
|
|
|
58
58
|
export const myGuard: NavigationGuard = async ({ to }) => {
|
|
59
59
|
if (to.config.blocked) {
|
|
60
|
-
return false; //
|
|
60
|
+
return false; // block this transition
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
if (to.config.redirect) {
|
|
64
|
-
return '/login/'; //
|
|
64
|
+
return '/login/'; // call a redirect to the specified page
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
//
|
|
67
|
+
// if nothing is returned, the transition will be performed as usual
|
|
68
68
|
};
|
|
69
69
|
```
|
|
70
70
|
|
|
71
|
-
###
|
|
71
|
+
### Transitions hooks
|
|
72
72
|
|
|
73
|
-
|
|
73
|
+
Transition hooks allow you to perform your asynchronous actions at different stages of the transition.
|
|
74
74
|
|
|
75
75
|
```ts
|
|
76
76
|
import { NavigationHook } from '@tinkoff/router';
|
|
@@ -82,27 +82,27 @@ export const myHook: NavigationHook = async ({ from, to, url, fromUrl }) => {
|
|
|
82
82
|
|
|
83
83
|
## API
|
|
84
84
|
|
|
85
|
-
###
|
|
85
|
+
### Getting data about the current route or url
|
|
86
86
|
|
|
87
87
|
```ts
|
|
88
|
-
router.getCurrentRoute(); //
|
|
89
|
-
router.getCurrentUrl(); //
|
|
88
|
+
router.getCurrentRoute(); // will return the current route
|
|
89
|
+
router.getCurrentUrl(); // will return the parsed version of the url of the current page
|
|
90
90
|
```
|
|
91
91
|
|
|
92
|
-
###
|
|
92
|
+
### Transition initiation
|
|
93
93
|
|
|
94
|
-
|
|
94
|
+
There are two methods for initializing the navigation and updating the address bar in the browser. The main difference between these two methods is that one of them will launch a full-fledged transition with data updating and starting heavy data loading actions. The second method is mainly used to update the state for the current route: to update the query parameters on the page or change the dynamic parameters of the route itself.
|
|
95
95
|
|
|
96
96
|
#### navigate
|
|
97
97
|
|
|
98
|
-
|
|
98
|
+
Initiates a full transition, defining the next route and updating the state in the browser.
|
|
99
99
|
|
|
100
100
|
```ts
|
|
101
101
|
router.navigate('/test');
|
|
102
102
|
router.navigate({ url: './test', query: { a: '1' } });
|
|
103
103
|
```
|
|
104
104
|
|
|
105
|
-
|
|
105
|
+
Transition hooks:
|
|
106
106
|
|
|
107
107
|
- beforeResolve
|
|
108
108
|
- beforeNavigate
|
|
@@ -110,23 +110,23 @@ router.navigate({ url: './test', query: { a: '1' } });
|
|
|
110
110
|
|
|
111
111
|
#### updateCurrentRoute
|
|
112
112
|
|
|
113
|
-
|
|
113
|
+
The transition is based on the current route (therefore this method cannot be called on the server) and allows you to simply update some data for the current page
|
|
114
114
|
|
|
115
115
|
```ts
|
|
116
116
|
router.updateCurrentRoute({ params: { id: 'abc' } });
|
|
117
117
|
router.updateCurrentRoute({ query: { a: '1' } });
|
|
118
118
|
```
|
|
119
119
|
|
|
120
|
-
|
|
120
|
+
Hooks:
|
|
121
121
|
|
|
122
122
|
- beforeUpdateCurrent
|
|
123
123
|
- afterUpdateCurrent
|
|
124
124
|
|
|
125
|
-
###
|
|
125
|
+
### Working with query
|
|
126
126
|
|
|
127
|
-
####
|
|
127
|
+
#### query option
|
|
128
128
|
|
|
129
|
-
|
|
129
|
+
Allows you to set a search string for an url as an object via the `query` option when navigating. The previous query value will be cleared
|
|
130
130
|
|
|
131
131
|
```ts
|
|
132
132
|
router.getCurrentUrl().query; // { с: 'c' }
|
|
@@ -139,7 +139,7 @@ router.getCurrentUrl().query; // { a: 'a', b: 'b' }
|
|
|
139
139
|
|
|
140
140
|
#### preserveQuery
|
|
141
141
|
|
|
142
|
-
|
|
142
|
+
Allows you to keep the query value from the current navigation and use them in a new transition
|
|
143
143
|
|
|
144
144
|
```ts
|
|
145
145
|
router.getCurrentUrl().query; // { с: 'c' }
|
|
@@ -150,7 +150,7 @@ router.updateCurrentRoute({ query: { a: 'a' }, preserveQuery: true });
|
|
|
150
150
|
router.getCurrentUrl().query; // { a: 'a', c: 'c' }
|
|
151
151
|
```
|
|
152
152
|
|
|
153
|
-
|
|
153
|
+
If you pass undefined as the value for a specific query key, then this value will be cleared in a new query:
|
|
154
154
|
|
|
155
155
|
```ts
|
|
156
156
|
router.getCurrentUrl().query; // { a: 'a', b: 'b' }
|
|
@@ -161,13 +161,13 @@ router.updateCurrentRoute({ query: { a: undefined, c: 'c' }, preserveQuery: true
|
|
|
161
161
|
router.getCurrentUrl().query; // { b: 'b', c: 'c' }
|
|
162
162
|
```
|
|
163
163
|
|
|
164
|
-
###
|
|
164
|
+
### Integration with React
|
|
165
165
|
|
|
166
|
-
|
|
166
|
+
Library has some useful React hooks and components for working with routing
|
|
167
167
|
|
|
168
168
|
#### useRoute
|
|
169
169
|
|
|
170
|
-
|
|
170
|
+
Returns current active route of the application
|
|
171
171
|
|
|
172
172
|
```ts
|
|
173
173
|
import React from 'react';
|
|
@@ -182,7 +182,7 @@ export const Component = () => {
|
|
|
182
182
|
|
|
183
183
|
#### useUrl
|
|
184
184
|
|
|
185
|
-
|
|
185
|
+
Returns current active URL of the application
|
|
186
186
|
|
|
187
187
|
```ts
|
|
188
188
|
import React from 'react';
|
|
@@ -197,7 +197,7 @@ export const Component = () => {
|
|
|
197
197
|
|
|
198
198
|
#### useNavigate
|
|
199
199
|
|
|
200
|
-
|
|
200
|
+
Creates a callback with a navigation call that can be passed to child components or used as an event handler
|
|
201
201
|
|
|
202
202
|
```ts
|
|
203
203
|
export const Cmp = () => {
|
|
@@ -209,9 +209,9 @@ export const Cmp = () => {
|
|
|
209
209
|
|
|
210
210
|
#### Link
|
|
211
211
|
|
|
212
|
-
|
|
212
|
+
A wrapper for a react component that makes it clickable
|
|
213
213
|
|
|
214
|
-
>
|
|
214
|
+
> If the react component is passed to the Link as children, then this passed component will be rendered and the `href`, `onClick` props will be passed as props to that component and they should be used to make the navigation. Otherwise, the `<a>` tag will be rendered with children as a child.
|
|
215
215
|
|
|
216
216
|
```ts
|
|
217
217
|
import { Link } from '@tinkoff/router';
|
package/lib/index.browser.js
CHANGED
|
@@ -183,6 +183,7 @@ class AbstractRouter {
|
|
|
183
183
|
}
|
|
184
184
|
this.runSyncHooks('change', navigation);
|
|
185
185
|
this.lastNavigation = navigation;
|
|
186
|
+
this.currentNavigation = null;
|
|
186
187
|
}
|
|
187
188
|
async updateCurrentRoute(updateRouteOptions) {
|
|
188
189
|
return this.internalUpdateCurrentRoute(updateRouteOptions, {});
|
|
@@ -276,7 +277,6 @@ class AbstractRouter {
|
|
|
276
277
|
if (navigation.type === 'updateCurrentRoute') {
|
|
277
278
|
await this.runUpdateCurrentRoute(navigation);
|
|
278
279
|
}
|
|
279
|
-
this.currentNavigation = null;
|
|
280
280
|
}
|
|
281
281
|
resolve(resolveOptions, options) {
|
|
282
282
|
const opts = typeof resolveOptions === 'string' ? { url: resolveOptions } : resolveOptions;
|
|
@@ -1164,4 +1164,4 @@ function Link(props) {
|
|
|
1164
1164
|
}
|
|
1165
1165
|
Link.displayName = 'Link';
|
|
1166
1166
|
|
|
1167
|
-
export { AbstractRouter, History, Link, NoSpaRouter, Provider, RouteTree, Router, logger, setLogger, useNavigate, useRoute, useRouter, useUrl };
|
|
1167
|
+
export { AbstractRouter, History, Link, NoSpaRouter, Provider, RouteTree, Router, getParts, isHistoryFallback, isParameterized, isWildcard, logger, makePath, parse, setLogger, useNavigate, useRoute, useRouter, useUrl };
|
package/lib/index.d.ts
CHANGED
package/lib/index.es.js
CHANGED
|
@@ -183,6 +183,7 @@ class AbstractRouter {
|
|
|
183
183
|
}
|
|
184
184
|
this.runSyncHooks('change', navigation);
|
|
185
185
|
this.lastNavigation = navigation;
|
|
186
|
+
this.currentNavigation = null;
|
|
186
187
|
}
|
|
187
188
|
async updateCurrentRoute(updateRouteOptions) {
|
|
188
189
|
return this.internalUpdateCurrentRoute(updateRouteOptions, {});
|
|
@@ -276,7 +277,6 @@ class AbstractRouter {
|
|
|
276
277
|
if (navigation.type === 'updateCurrentRoute') {
|
|
277
278
|
await this.runUpdateCurrentRoute(navigation);
|
|
278
279
|
}
|
|
279
|
-
this.currentNavigation = null;
|
|
280
280
|
}
|
|
281
281
|
resolve(resolveOptions, options) {
|
|
282
282
|
const opts = typeof resolveOptions === 'string' ? { url: resolveOptions } : resolveOptions;
|
|
@@ -1096,4 +1096,4 @@ function Link(props) {
|
|
|
1096
1096
|
}
|
|
1097
1097
|
Link.displayName = 'Link';
|
|
1098
1098
|
|
|
1099
|
-
export { AbstractRouter, History, Link, NoSpaRouter, Provider, RouteTree, Router, logger, setLogger, useNavigate, useRoute, useRouter, useUrl };
|
|
1099
|
+
export { AbstractRouter, History, Link, NoSpaRouter, Provider, RouteTree, Router, getParts, isHistoryFallback, isParameterized, isWildcard, logger, makePath, parse, setLogger, useNavigate, useRoute, useRouter, useUrl };
|
package/lib/index.js
CHANGED
|
@@ -199,6 +199,7 @@ class AbstractRouter {
|
|
|
199
199
|
}
|
|
200
200
|
this.runSyncHooks('change', navigation);
|
|
201
201
|
this.lastNavigation = navigation;
|
|
202
|
+
this.currentNavigation = null;
|
|
202
203
|
}
|
|
203
204
|
async updateCurrentRoute(updateRouteOptions) {
|
|
204
205
|
return this.internalUpdateCurrentRoute(updateRouteOptions, {});
|
|
@@ -292,7 +293,6 @@ class AbstractRouter {
|
|
|
292
293
|
if (navigation.type === 'updateCurrentRoute') {
|
|
293
294
|
await this.runUpdateCurrentRoute(navigation);
|
|
294
295
|
}
|
|
295
|
-
this.currentNavigation = null;
|
|
296
296
|
}
|
|
297
297
|
resolve(resolveOptions, options) {
|
|
298
298
|
const opts = typeof resolveOptions === 'string' ? { url: resolveOptions } : resolveOptions;
|
|
@@ -1119,6 +1119,12 @@ exports.NoSpaRouter = NoSpaRouter;
|
|
|
1119
1119
|
exports.Provider = Provider;
|
|
1120
1120
|
exports.RouteTree = RouteTree;
|
|
1121
1121
|
exports.Router = Router;
|
|
1122
|
+
exports.getParts = getParts;
|
|
1123
|
+
exports.isHistoryFallback = isHistoryFallback;
|
|
1124
|
+
exports.isParameterized = isParameterized;
|
|
1125
|
+
exports.isWildcard = isWildcard;
|
|
1126
|
+
exports.makePath = makePath;
|
|
1127
|
+
exports.parse = parse;
|
|
1122
1128
|
exports.setLogger = setLogger;
|
|
1123
1129
|
exports.useNavigate = useNavigate;
|
|
1124
1130
|
exports.useRoute = useRoute;
|
package/lib/types.d.ts
CHANGED
|
@@ -13,8 +13,8 @@ export interface NavigationRoute extends Route {
|
|
|
13
13
|
navigateState?: any;
|
|
14
14
|
}
|
|
15
15
|
export interface BaseNavigateOptions {
|
|
16
|
-
params?: Params
|
|
17
|
-
query?: Query
|
|
16
|
+
params?: Partial<Params>;
|
|
17
|
+
query?: Partial<Query>;
|
|
18
18
|
preserveQuery?: boolean;
|
|
19
19
|
replace?: boolean;
|
|
20
20
|
hash?: string;
|
package/package.json
CHANGED
package/README.en.md
DELETED
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
# @tinkoff/router
|
|
2
|
-
|
|
3
|
-
Routing library. It can work both on the server and on the client. Designed primarily for building ismorphic applications.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
You need to install `@tinkoff/router`:
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
yarn add @tinkoff/router
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
And connect in the project:
|
|
14
|
-
|
|
15
|
-
```tsx
|
|
16
|
-
import { Router } from '@tinkoff/router';
|
|
17
|
-
|
|
18
|
-
const router = new Router();
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
## Explanation
|
|
22
|
-
|
|
23
|
-
Features:
|
|
24
|
-
|
|
25
|
-
- The library supports options for working both on the server and on the client.
|
|
26
|
-
- It is possible to use different client transition options: with or without SPA transitions.
|
|
27
|
-
- There are Guards to check the availability of a route under specific conditions.
|
|
28
|
-
- You can subscribe to different stages of the transition through hooks
|
|
29
|
-
- Components and hooks for easy routing from react
|
|
30
|
-
|
|
31
|
-
### Server and client version
|
|
32
|
-
|
|
33
|
-
It is enough just to import routing from the library itself and, based on the settings, the required version for the server or client will be returned to package.json
|
|
34
|
-
|
|
35
|
-
```ts
|
|
36
|
-
import { Router } from '@tinkoff/router';
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
### Client routing with/without SPA transitions
|
|
40
|
-
|
|
41
|
-
By default, routing with SPA transitions enabled is exported on the client.
|
|
42
|
-
If you need to disable SPA transitions, you need to import a special version of the routing
|
|
43
|
-
|
|
44
|
-
```ts
|
|
45
|
-
import { Router, SpaHistory } from '@tinkoff/router';
|
|
46
|
-
import { NoSpaRouter } from '@tinkoff/router';
|
|
47
|
-
|
|
48
|
-
const spaRouter = new Router({ history: new SpaHistory() });
|
|
49
|
-
const noSpaRouter = new NoSpaRouter();
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
### Router Guards
|
|
53
|
-
|
|
54
|
-
Guards allow you to control the availability of a particular route for a specific transition.
|
|
55
|
-
From the guard, you can block the transition or initiate a redirect.
|
|
56
|
-
|
|
57
|
-
```ts
|
|
58
|
-
import { NavigationGuard } from '@tinkoff/router';
|
|
59
|
-
|
|
60
|
-
export const myGuard: NavigationGuard = async ({ to }) => {
|
|
61
|
-
if (to.config.blocked) {
|
|
62
|
-
return false; // block this transition
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (to.config.redirect) {
|
|
66
|
-
return '/login/'; // call a redirect to the specified page
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// if nothing is returned, the transition will be performed as usual
|
|
70
|
-
};
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
### Transitions hooks
|
|
74
|
-
|
|
75
|
-
Transition hooks allow you to perform your asynchronous actions at different stages of the transition.
|
|
76
|
-
|
|
77
|
-
```ts
|
|
78
|
-
import { NavigationHook } from '@tinkoff/router';
|
|
79
|
-
|
|
80
|
-
export const myHook: NavigationHook = async ({ from, to, url, fromUrl }) => {
|
|
81
|
-
console.log(`navigating from ${from} to route ${to}`);
|
|
82
|
-
};
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
## API
|
|
86
|
-
|
|
87
|
-
### Getting data about the current route or url
|
|
88
|
-
|
|
89
|
-
```ts
|
|
90
|
-
router.getCurrentRoute(); // will return the current route
|
|
91
|
-
router.getCurrentUrl(); // will return the parsed version of the url of the current page
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
### Transition initiation
|
|
95
|
-
|
|
96
|
-
There are two methods for initializing the navigation and updating the address bar in the browser. The main difference between these two methods is that one of them must launch a full-fledged transition with data updating and starting heavy data loading actions. The second method is mainly used to update the state for the current route: to update the query parameters on the page or change the dynamic parameters of the route itself.
|
|
97
|
-
|
|
98
|
-
#### navigate
|
|
99
|
-
|
|
100
|
-
Initiates a full transition, defining the next route and updating the state in the browser.
|
|
101
|
-
|
|
102
|
-
```ts
|
|
103
|
-
router.navigate('/test');
|
|
104
|
-
router.navigate({ url: './test', query: { a: '1' } });
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
Transition hooks:
|
|
108
|
-
|
|
109
|
-
- beforeResolve
|
|
110
|
-
- beforeNavigate
|
|
111
|
-
- afterNavigate
|
|
112
|
-
|
|
113
|
-
#### updateCurrentRoute
|
|
114
|
-
|
|
115
|
-
The transition is based on the current route (therefore this method cannot be called on the server) and allows you to simply update some data for the current page
|
|
116
|
-
|
|
117
|
-
```ts
|
|
118
|
-
router.updateCurrentRoute({ params: { id: 'abc' } });
|
|
119
|
-
router.updateCurrentRoute({ query: { a: '1' } });
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
Hooks:
|
|
123
|
-
|
|
124
|
-
- beforeUpdateCurrent
|
|
125
|
-
- afterUpdateCurrent
|
|
126
|
-
|
|
127
|
-
### Working with query
|
|
128
|
-
|
|
129
|
-
#### query option
|
|
130
|
-
|
|
131
|
-
Allows you to set a search string for an url as an object via the `query` option when navigating. The previous query value will be cleared
|
|
132
|
-
|
|
133
|
-
```ts
|
|
134
|
-
router.getCurrentUrl().query; // { с: 'c' }
|
|
135
|
-
|
|
136
|
-
router.navigate({ query: { a: 'a', b: 'b' } });
|
|
137
|
-
router.updateCurrentRoute({ query: { a: 'a', b: 'b' } });
|
|
138
|
-
|
|
139
|
-
router.getCurrentUrl().query; // { a: 'a', b: 'b' }
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
#### preserveQuery
|
|
143
|
-
|
|
144
|
-
Allows you to keep the query value from the current navigation and use them in a new transition
|
|
145
|
-
|
|
146
|
-
```ts
|
|
147
|
-
router.getCurrentUrl().query; // { с: 'c' }
|
|
148
|
-
|
|
149
|
-
router.navigate({ query: { a: 'a' }, preserveQuery: true });
|
|
150
|
-
router.updateCurrentRoute({ query: { a: 'a' }, preserveQuery: true });
|
|
151
|
-
|
|
152
|
-
router.getCurrentUrl().query; // { a: 'a', c: 'c' }
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
If you pass undefined as the value for a specific query key, then this value will be cleared in a new query:
|
|
156
|
-
|
|
157
|
-
```ts
|
|
158
|
-
router.getCurrentUrl().query; // { a: 'a', b: 'b' }
|
|
159
|
-
|
|
160
|
-
router.navigate({ query: { a: undefined, c: 'c' }, preserveQuery: true });
|
|
161
|
-
router.updateCurrentRoute({ query: { a: undefined, c: 'c' }, preserveQuery: true });
|
|
162
|
-
|
|
163
|
-
router.getCurrentUrl().query; // { b: 'b', c: 'c' }
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
### Integration with React
|
|
167
|
-
|
|
168
|
-
Has some useful React hooks and components for working with routing
|
|
169
|
-
|
|
170
|
-
#### useRoute
|
|
171
|
-
|
|
172
|
-
Allows you to get the current active route of the application
|
|
173
|
-
|
|
174
|
-
```ts
|
|
175
|
-
import React from 'react';
|
|
176
|
-
import { useRoute } from '@tinkoff/router';
|
|
177
|
-
|
|
178
|
-
export const Component = () => {
|
|
179
|
-
const route = useRoute();
|
|
180
|
-
|
|
181
|
-
return <div>Route path: {route.actualPath}</div>;
|
|
182
|
-
};
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
#### useUrl
|
|
186
|
-
|
|
187
|
-
Lets get the current active URL of the application
|
|
188
|
-
|
|
189
|
-
```ts
|
|
190
|
-
import React from 'react';
|
|
191
|
-
import { useUrl } from '@tinkoff/router';
|
|
192
|
-
|
|
193
|
-
export const Component = () => {
|
|
194
|
-
const url = useUrl();
|
|
195
|
-
|
|
196
|
-
return <div>Url query: {JSON.stringify(url.query)}</div>;
|
|
197
|
-
};
|
|
198
|
-
```
|
|
199
|
-
|
|
200
|
-
#### useNavigate
|
|
201
|
-
|
|
202
|
-
Creates a callback with a navigation call that can be passed to child components or hung as an event handler
|
|
203
|
-
|
|
204
|
-
```ts
|
|
205
|
-
export const Cmp = () => {
|
|
206
|
-
const navigate = useNavigate('/test/');
|
|
207
|
-
|
|
208
|
-
return <div onClick={navigate}>Test</div>;
|
|
209
|
-
};
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
#### Link
|
|
213
|
-
|
|
214
|
-
A wrapper for a react component that makes it clickable
|
|
215
|
-
|
|
216
|
-
> If the react component is passed to the Link as children, then this passed component will be rendered and the `href`, `onClick` props that need to be used to call the navigation will be passed to it. Otherwise, the `<a>` tag will be rendered with children as a child.
|
|
217
|
-
|
|
218
|
-
```ts
|
|
219
|
-
import { Link } from '@tinkoff/router';
|
|
220
|
-
import CustomLink from '@custom-scope/link';
|
|
221
|
-
|
|
222
|
-
export const Component = () => {
|
|
223
|
-
return (
|
|
224
|
-
<Link url="/test/">
|
|
225
|
-
<CustomLink />
|
|
226
|
-
</Link>
|
|
227
|
-
);
|
|
228
|
-
};
|
|
229
|
-
|
|
230
|
-
export const WrapLink = () => {
|
|
231
|
-
return <Link url="/test/">Click me</Link>;
|
|
232
|
-
};
|
|
233
|
-
```
|