Skip to content

Parent route query params disappear when visiting child route #8179

Closed Answered by kiliman
kevinreber asked this question in Q&A
Discussion options

You must be logged in to vote

That's not how URLs and query/search params work. The search params need to be at the end of the path: /parent/child123?q=foobar

The parent route can still access them, since they apply to the entire route, not just a single route segment.

You can create a custom <Link> component that always preserves the search params.

import { Link, type LinkProps, useLocation } from '@remix-run/react'

type MyLinkProps = LinkProps & { preserveSearch: boolean }

export function MyLink({
  to,
  preserveSearch,
  children,
  ...props
}: MyLinkProps) {
  const location = useLocation()
  return (
    <Link {...props} to={`${to}${preserveSearch ? location.search : ''}`}>
      {children}
    </Link>
  )
}

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
3 replies
@kevinreber
Comment options

@kiliman
Comment options

Answer selected by kevinreber
@kevinreber
Comment options

Comment options

You must be logged in to vote
1 reply
@kiliman
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants