Skip to content

isDefaultState function #860

Answered by franky47
kodobrody asked this question in Ideas
Jan 14, 2025 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

Here's an example:

'use client'

import { type inferParserType, parseAsFloat, type ParserMap, useQueryStates } from 'nuqs'
import { Suspense } from 'react'

export default function Page() {
  return (
    <Suspense>
      <Client />
    </Suspense>
  )
}

const searchParams = {
  lat: parseAsFloat.withDefault(0),
  lng: parseAsFloat.withDefault(0)
}

function isDefaultState<Parsers extends ParserMap>(
  parsers: Parsers,
  values: inferParserType<Parsers>
): boolean {
  for (const [key, parser] of Object.entries(parsers)) {
    if (!parser.eq(values[key], parsers[key].defaultValue)) {
      return false
    }
  }
  return true
}

function Client() {
  const [state, setState] = useQueryStates

Replies: 1 comment 2 replies

Comment options

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

@franky47
Comment options

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