Skip to content

Keep Search Params on NextJS Link Navigation #766

Answered by franky47
tariwiencke asked this question in Q&A
Discussion options

You must be logged in to vote

You can leverage the serializer helper along with Next's stock useSearchParams to generate links that persist the current search params:

import { MapControls, MapView } from '@/components/test';
import { createSerializer, type inferParserType } from 'nuqs/server'
import Link from 'next/link';
import { useSearchParams } from 'next/navigation'

// Import the same parsers as you used in useCoordinates
const serialize = createSerializer(coordinatesParsers)

function usePersistedSearchParamsLink(
  base: string, 
  values: Partial<inferParserType<typeof coordinateParsers>> = {}
) {
  const searchParams = useSearchParams()
  return base + serialize(searchParams, values)
}

export default function 

Replies: 2 comments 4 replies

Comment options

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

@franky47
Comment options

Answer selected by tariwiencke
Comment options

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

@tariwiencke
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
2 participants