Skip to content

Any examples with router.push? #452

Answered by franky47
cpotey asked this question in Q&A
Jan 10, 2024 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

There are a couple of ways nuqs can help you here.

First is the history option, that can tell nuqs to push a new entry in the History. Combine it with shallow: false to use the Next.js router to send an update to the server, and you get the same behaviour as your example:

const [selectedFormats, setSelectedFormats] = useQueryState(
    'format',
    parseAsArrayOf(parseAsString)
      .withDefault([])
      .withOptions({ 
        shallow: false, // Send updates to the server
        history: 'push' // And push a new entry in the History API while doing so
      })
);
  
const handleToggle = (format: string[]) => {
    setSelectedFormats(format.length > 0 ? format : null);
};

This might w…

Replies: 1 comment 2 replies

Comment options

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

@franky47
Comment options

Answer selected by franky47
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