Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: useSearchParamState #9

Open
iamnafets opened this issue Feb 5, 2024 · 3 comments
Open

Suggestion: useSearchParamState #9

iamnafets opened this issue Feb 5, 2024 · 3 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@iamnafets
Copy link

iamnafets commented Feb 5, 2024

Sometimes it's useful to use the search params to store state. We currently have something like this:

    const [query, setQuery] = useSearchParamState('query', z.string(), "defaultQuery");

Where setQuery actually changes the search params and updates the state. I'd love to incorporate this into the library. Any interest?

Thinking this look like:

export const { routes, useSafeParams, useSearchParamsState } = createNavigationConfig(
   // ...
)

const [{ query, page }, setSearchParams] = useSearchParamsState('search');
@lukemorales
Copy link
Owner

Thanks for another great suggestion @iamnafets!

Could you elaborate a bit more over how would this be different/better than using:

export const { routes, useSafeParams, useSearchParamsState } = createNavigationConfig(defineRoute => ({
   search: defineRoute('/search', { search: z.object({ query: z.string(), page: z.coerce.number() })  })
}));

// Search Page

export default function SeachPage() {
  // ...
  return (
    <Pagination>
      <Link href={routes.search({ search: { query, page: page + 1 } })}>Next page</Link>
    </Pagination>
  )
}

@lukemorales lukemorales added enhancement New feature or request question Further information is requested labels Feb 5, 2024
@iamnafets
Copy link
Author

For SPAs, you often want to manipulate the querystring without navigating. React's Dispatch<SetActionState<T>> takes a lambda which passes the current value allowing you to do something like setSearchParams(old => ({ ...old, page: old.page +1 })) instead of re-assembling the whole object for if your queryString becomes large.

@lukemorales
Copy link
Owner

lukemorales commented Feb 5, 2024

For SPAs, you often want to manipulate the querystring without navigating.

How come? I mean, what's the point of storing state in the URL if you're not navigating?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants