Skip to content

custom parser for multiple keys #463

Answered by franky47
tonyxiao asked this question in Ideas
Discussion options

You must be logged in to vote

Do you have an example?

You can kind of get there with useQueryStates, and declaring an object of parsers for the individual keys:

const searchParamsParsers = {
  page: parseAsInteger.withDefault(1),
  modalOpen: parseAsBoolean.withDefault(false),
  search: parseAsString
}

const [{ page, modalOpen, search }, updateSearchParams] = useQueryStates(searchParamsParsers)

const nextPage = () => updateSearchParams(state => ({
  page: state.page + 1
  // Partial update: other search params keep their values
}))

One downside to this is that every key is parsed individually. It would not help in cases where you need key foo to have a particular set of values when bar has a specific value. Adding a…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by franky47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
2 participants