UNPKG

nuqs

Version:

Type-safe search params state manager for Next.js - Like React.useState, but stored in the URL query string

39 lines (36 loc) 1.15 kB
import { renderURL } from './chunk-TUUFGL5M.js'; import { debug } from './chunk-3RCMAOX7.js'; import { useRouter, useSearchParams } from 'next/navigation'; import { useCallback } from 'react'; function useNuqsNextAppRouterAdapter() { const router = useRouter(); const searchParams = useSearchParams(); const updateUrl = useCallback((search, options) => { const url = renderURL(location.origin + location.pathname, search); debug("[nuqs queue (app)] Updating url: %s", url); const updateMethod = options.history === "push" ? history.pushState : history.replaceState; updateMethod.call( history, // In next@14.1.0, useSearchParams becomes reactive to shallow updates, // but only if passing `null` as the history state. null, "", url ); if (options.scroll) { window.scrollTo(0, 0); } if (!options.shallow) { router.replace(url, { scroll: false }); } }, []); return { searchParams, updateUrl, // See: https://github.com/47ng/nuqs/issues/603#issuecomment-2317057128 rateLimitFactor: 2 }; } export { useNuqsNextAppRouterAdapter };