Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useRoutePath(): Get the path for the current route by default #9790

Merged
merged 5 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions docs/docs/router.md
Original file line number Diff line number Diff line change
Expand Up @@ -478,14 +478,24 @@ Example output:

## useRoutePath

This is a convenience hook for when you only want the path for a single route.
Use this hook when you only want the path for a single route. By default it
will give you the path for the current route
```jsx
const aboutPath = useRoutePath('about') // returns "/about"
// returns "/about" if you're currently on https://example.org/about
const aboutPath = useRoutePath()
```
is the same as

You can also pass in the name of a route and get the path for that route
```jsx
// returns "/about"
const aboutPath = useRoutePath('about')
```

Note that the above is the same as
```jsx
const routePaths = useRoutePaths()
const aboutPath = routePaths.about // Also returns "/about"
// returns "/about"
const aboutPath = routePaths.about
```

## useRouteName
Expand Down
25 changes: 21 additions & 4 deletions packages/router/src/__tests__/useRoutePaths.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import React from 'react'

import { render } from '@testing-library/react'
import { act } from 'react-dom/test-utils'

import { navigate } from '../history'
import { Route, Router } from '../router'
import { Set } from '../Set'
import { useRoutePaths, useRoutePath } from '../useRoutePaths'
Expand All @@ -27,17 +29,25 @@ test('useRoutePaths and useRoutePath', async () => {
children: React.ReactNode
}

const Layout = ({ children }: LayoutProps) => <>{children}</>
const Layout = ({ children }: LayoutProps) => {
// No name means current route
const routePath = useRoutePath()

return (
<>
<h1>Current route path: &quot;{routePath}&quot;</h1>
{children}
</>
)
}

const Page = () => <h1>Page</h1>

const TestRouter = () => (
<Router>
<Route path="/" page={HomePage} name="home" />
<Set wrap={Layout}>
<Route path="/" page={HomePage} name="home" />
<Route path="/one" page={Page} name="one" />
</Set>
<Set wrap={Layout}>
<Route path="/two/{id:Int}" page={Page} name="two" />
</Set>
</Router>
Expand All @@ -48,4 +58,11 @@ test('useRoutePaths and useRoutePath', async () => {
await screen.findByText('Home Page')
await screen.findByText(/^My path is\s+\/$/)
await screen.findByText(/^All paths:\s+\/,\/one,\/two\/\{id:Int\}$/)
await screen.findByText('Current route path: "/"')

act(() => navigate('/one'))
await screen.findByText('Current route path: "/one"')

act(() => navigate('/two/123'))
await screen.findByText('Current route path: "/two/{id:Int}"')
})
12 changes: 10 additions & 2 deletions packages/router/src/useRoutePaths.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useRouterState } from './router-context'
import { useRouteName } from './useRouteName'
import type { GeneratedRoutesMap } from './util'

import type { AvailableRoutes } from '.'
Expand All @@ -20,8 +21,15 @@ export function useRoutePaths() {
return routePaths
}

export function useRoutePath(routeName: keyof AvailableRoutes) {
export function useRoutePath(routeName?: keyof AvailableRoutes) {
const currentRouteName = useRouteName()
const routePaths = useRoutePaths()

return routePaths[routeName]
const name = routeName || currentRouteName

if (!name) {
return undefined
}

return routePaths[name]
}
Loading