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

Allow useMatch take RouteDefinition object instead of path. #39

Open
minht11 opened this issue Aug 28, 2021 · 1 comment
Open

Allow useMatch take RouteDefinition object instead of path. #39

minht11 opened this issue Aug 28, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@minht11
Copy link

minht11 commented Aug 28, 2021

Basically I want to know which route is currently active outside of <Routes/>, so I can for example change nav bar styles on specific page, currently this is hard to do correctly. So my proposed solution is to allow useMatch take RouteDefinition object so this works:

const LIST_ROUTE = {
  path: '/list',
  component: List,
  children: [
    { path: '/' },
    { path: '/apples' },
    { path: '/oranges' },
    { path: '/banana' },
  ],
}

const routes: RouteDefinition[] = [
  {
    path: '/',
    component: Home,
  },
  LIST_ROUTE,
]

export const App = () => {
  const Routes = useRoutes(routes)
  const isListRoute = useMatch(LIST_ROUTE)

  return (
    <div>
      <Routes />
      <BottomNavBar hide={isListRoute()} />
    </div>
  )
}

Alternative today is to use

const isListRoute = useMatch(() => '/list/*') // Matches everything and is not a correct solution
// or way more complicated
const isListRoute = useMatch(() => '/list/')
const isListAppleRoute = useMatch(() => '/list/apple') 
const isListOrangeRoute = useMatch(() => '/list/orange') 
const isListBananaRoute = useMatch(() => '/list/banana') 
const isListRouteActive= createMemo(() => isListRoute() && isListAppleRoute() && isListOrangeRoute() && isListBananaRoute())

There might be some other way to solve this like having <Routes /> fire change event with matched route id.

@ryansolid ryansolid added the enhancement New feature or request label Nov 21, 2022
@orenelbaum
Copy link
Contributor

I think that this API is a bit awkward and I'd probably prefer something like this

useMatch('/list', { matchChildren: true })
useMatch('/list', { matchChildren: true, matchChildrenOnly: true })

See also this issue #127
According to the original proposal it could be

useMatch(path`/list/${['apple', 'orange', 'banana']}`)

Or the way I'd prefer, something like

useMatch('/list/(apple|orange|banana)')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants