the state variable to sync
setter function to update the state variable (should be memoized with useCallback)
the key of the URL search parameter to sync with
the current URL search parameter value (i.e., from useSearchParams)
function to update the URL search parameters (i.e., from useSearchParams)
function to check if the underlying state is initialized and ready to sync with URL (should be memoized with useCallback)
Util hook to keep a state variable in sync with a URL search parameter. This hook syncs from URL to state if the state is null/undefined (so initial value is set from URL) and from state to URL otherwise.
This hook properly queues setSearchParams calls to ensure all updates are applied, working around react-router's limitation where multiple calls to setSearchParams in the same tick don't build on each other. See: https://github.com/remix-run/react-router/issues/9304