Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Optional params? #146

Answered by tannerlinsley
Maadtin asked this question in Q&A
Dec 9, 2021 · 4 comments · 11 replies
Discussion options

In react router v6 they are no longer supported.

for example clients/:id? would match both /clients and /clients/1

Is there a way to achieve something like this in react location?

You must be logged in to vote

Replies: 4 comments · 11 replies

Comment options

You must be logged in to vote
2 replies
@Moshyfawn
Comment options

So, is this the idiomatic RL way to have an optional param? To duplicate the code for both the root and the dynamic param route?

{
  path: 'teams',
  children: [
    {
      path: '/',
      element: <Teams />
    },
    {
      path: ':id',
      element: <Teams />
    }
  ]
}

I'm looking for a solution to be able to get the optional parameter and, say, select a row with the selected data row ID from the URL. So, this way the same <Teams /> component would render on both teams and teams/:id paths

const { params: { id: teamId } } = useMatch()

useEffect(() => {
  if (teamId) {
    tableRef.current?.scrollToRow(teamId)
  }
}, [teamId])

Here's an example CSB

@DCzajkowski
Comment options

I think this is the way, however this will cause Teams to fully unmount and remount during navigation between / and /$id. This can break focus etc.

I think the only way to do this without a full remount is to use splat routes.

Answer selected by Maadtin
Comment options

@tannerlinsley
is there a built in for that in v1?

or I would have to declare 2 two routes, one with the param and one without it?

const route1  = new Route({
   path: 'user',
   ...
})

const route1  = new Route({
   path: 'user/$userID',
   ...
})
You must be logged in to vote
1 reply
@tannerlinsley
Comment options

Correct :)

Comment options

@tannerlinsley Is there a plan to make this more ergonomic? Something similar to react-router https://reactrouter.com/en/main/route/route#optional-segments?

It's really annoying at the moment to support that exact pattern from the react-router link above, that is to have a lang param that sits at the root of your routes.

/:lang?/some/really/long/path

You can declare two different routes, one for / and one for /lang - but that means you'd have to duplicate every other route that comes afterwards, which is very cumbersome (duplicate objects with different parent route if code base, otherwise duplicate files if the route is file-based).


An alternative is to redirect on "/" to "/en" (if you're doing i18n), similarly to tanstack.com:

https://tanstack.com/router/ -> redirects to https://tanstack.com/router/latest - works ✅
https://tanstack.com/router/v1 - works ✅

That would work really nice for the docs as well

https://tanstack.com/router/latest/docs/latest/framework/react/overview - works ✅
https://tanstack.com/router/v1/docs/latest/framework/react/overview - works ✅

https://tanstack.com/router/docs/framework/react/overview - doesn't work ❌
You must be logged in to vote
1 reply
@schiller-manuel
Comment options

eventually this is a feature that can be implemented, however it is not in our focus right now.
if you want to contribute via a PR, you are welcome!
also if you want to get this feature prioritized, reach out on discord to discuss sponsorship options.

Comment options

Our team is considering migrating from React Router to TanStack router, but this is the only blocker.

We have these paths

/skill
and
/skill/$skillId

We want to render the SkillPage component for two cases and type-safely use the parameters inside

const skillId = useParams({
  from: `skill/$skillId`,
  select: (params) => params.skillId,
});

This is how we determine whether the skill is created or not.

You must be logged in to vote
7 replies
@noudadrichem
Comment options

➕1 on having optional parameters in routes. We're migrating from react-router to TanStack Router as well because of its preloading functionality. But we have to create multiple routes for the same components to get optional route params to work.

Feels a bit weird.

@tannerlinsley
Comment options

We’re almost there.

@medihack
Comment options

Is there some issue or PR to follow? (We would like to use this feature for i18n routing.)

@Amixx
Comment options

Seconding medihack - is there a PR to follow? Looking forward to this feature!

@ilarramendi
Comment options

Also interested in following the process of this feature!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
Morty Proxy This is a proxified and sanitized view of the page, visit original site.