Skip to content

Commit 8417266

Browse files
authored
Merge pull request #5 from atomic-router/feat/use-link
2 parents d14e750 + 1cfe3de commit 8417266

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,21 @@ import { Route } from 'atomic-router-react'
7878

7979
<Route route={homeRoute} view={HomePage} />
8080
```
81+
82+
### `useLink` — resolve path from route
83+
84+
```tsx
85+
import { useLink } from 'atomic-router-react'
86+
import { createRoute } from 'atomic-router'
87+
88+
// example path: /some/route/:someId
89+
const someRoute = createRoute<{ someId: number }>()
90+
91+
function SomeComponent() {
92+
const path = useLink(someRoute, { someId: 1 })
93+
// -> /some/route/1
94+
}
95+
```
96+
97+
Be sure, route is passed into `routes` for `createHistoryRoutes`.
98+
Else hook will throw `[useLink] Route not found`.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"atomic-router": "^0.6.1",
7171
"effector": "^22.3.0",
7272
"effector-react": "^22.1.6",
73+
"history": "^5.3.0",
7374
"jest": "^28.1.0",
7475
"jest-environment-jsdom": "^28.1.3",
7576
"prettier": "^2.7.1",

src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from './route';
33
export * from './router-provider';
44
export * from './create-route-view';
55
export * from './create-routes-view';
6+
export * from './use-link'

src/use-link.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { buildPath, RouteInstance, RouteParams, RouteQuery } from "atomic-router";
2+
import { useRouter } from "./router-provider";
3+
4+
export function useLink<T extends RouteParams>(
5+
route: RouteInstance<T>,
6+
params: T,
7+
query: RouteQuery = {},
8+
): string {
9+
const router = useRouter()
10+
const routeObj = router.routes.find((routeObj) => routeObj.route === route);
11+
12+
if (!routeObj) {
13+
throw new Error(`[useLink] Route not found. Maybe it is not passed into createHistoryRouter`)
14+
}
15+
16+
return buildPath({
17+
pathCreator: routeObj.path,
18+
params: params,
19+
query: query,
20+
})
21+
}

yarn.lock

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@
973973
"@babel/helper-validator-option" "^7.18.6"
974974
"@babel/plugin-transform-typescript" "^7.18.6"
975975

976-
"@babel/runtime@^7.12.5", "@babel/runtime@^7.8.4", "@babel/runtime@^7.9.2":
976+
"@babel/runtime@^7.12.5", "@babel/runtime@^7.7.6", "@babel/runtime@^7.8.4", "@babel/runtime@^7.9.2":
977977
version "7.18.9"
978978
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.18.9.tgz#b4fcfce55db3d2e5e080d2490f608a3b9f407f4a"
979979
integrity sha512-lkqXDcvlFT5rvEjiu6+QYO+1GXrEHRo2LOtS7E4GtX5ESIZOgepqsZBVIj6Pv+a6zqsya9VCgiK1KAK4BvJDAw==
@@ -2351,6 +2351,13 @@ has@^1.0.3:
23512351
dependencies:
23522352
function-bind "^1.1.1"
23532353

2354+
history@^5.3.0:
2355+
version "5.3.0"
2356+
resolved "https://registry.yarnpkg.com/history/-/history-5.3.0.tgz#1548abaa245ba47992f063a0783db91ef201c73b"
2357+
integrity sha512-ZqaKwjjrAYUYfLG+htGaIIZ4nioX2L70ZUMIFysS3xvBsSG4x/n1V6TXV3N8ZYNuFGlDirFg32T7B6WOUPDYcQ==
2358+
dependencies:
2359+
"@babel/runtime" "^7.7.6"
2360+
23542361
html-encoding-sniffer@^3.0.0:
23552362
version "3.0.0"
23562363
resolved "https://registry.yarnpkg.com/html-encoding-sniffer/-/html-encoding-sniffer-3.0.0.tgz#2cb1a8cf0db52414776e5b2a7a04d5dd98158de9"

0 commit comments

Comments
 (0)