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

Feature proposal: clearOnNoQuery option for usePaginatedQuery #482

Merged
merged 2 commits into from
May 12, 2020

Conversation

JaminFarr
Copy link
Contributor

This PR adds a clearOnNoQuery option to usePaginatedQuery. When clearOnNoQuery is true resolvedData will clear if the query key is falsy.

I'm using usePaginatedQuery to provide the data for a search page with paginated results.

The issue I currently have is I want the results to clear when the search is cleared while still being able to paginate through the results. I set the query key to null when the search form is reset but the last searches results still in resolvedData.

The 2 workarounds that I've figured out before this PR are a bit hacky.

  1. A special query key that immediately resolves to a special no results value
const { resolvedData } = usePaginatedQuery(
  searchTerm ? ['search', searchTerm] : ['NO-SEARCH'],
  (queryKey) => {
    if (queryKey === 'NO-SEARCH') return Promise.resolve('NO-RESULTS')
  }
  // return real request
)
  1. Perform the request in a results component that is only mounted when there a search value. I feel this mixes the logic with the display too much.

@tannerlinsley
Copy link
Collaborator

Is there any reason why this shouldn't be the default?

@JaminFarr
Copy link
Contributor Author

Not that I know of.
I didn't want to break anyone else's use case.

@JaminFarr
Copy link
Contributor Author

Updated to the default behavior

@tannerlinsley
Copy link
Collaborator

Nice! Yeah I think that's a good default.

@tannerlinsley tannerlinsley merged commit 2d7803f into TanStack:master May 12, 2020
@tannerlinsley
Copy link
Collaborator

🎉 This PR is included in version 1.4.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

@JaminFarr JaminFarr deleted the feature/clear-falsy-query branch May 13, 2020 08:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants