Skip to content

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
Member

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
Member

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

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

🎉 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
@nx-cloud
Copy link

nx-cloud bot commented Sep 23, 2025

View your CI Pipeline Execution ↗ for commit 35a97b8

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded <1s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 2s View ↗

☁️ Nx Cloud last updated this comment at 2025-09-23 11:28:25 UTC

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