-
I currently use queryClient.fetchQuery to fetch a query and store the data in a reducer. I would like to get the loading status of fetchQuery. useQuery has a loading status, but there isn't a promise function like fetchQuery returned from useQuery. There is refetch, but that will discard old data. There is onSuccess, but that will only be called when there is new data. fetchQuery returns data in every case and loads data from cache. I am thinking of a couple of options:
Is there part of the library I've overlooked that will show me the loading status of a query. Option 1 seems best, because it seems redundant to create a state, when react-query already stores the loading state. Is it overkill to use the useQuery hook just to get the loading status? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
I think you are missing the point of react-query a bit if you use it to fetch data and then store it in a state manager like redux or useReducer. react-query is the state manager! The idea is that you just call Please have a look at the various examples we have, e.g. https://react-query.tanstack.com/examples/basic |
Beta Was this translation helpful? Give feedback.
-
Cool. Incidentally, I have written about this exact use case in my blog: https://tkdodo.eu/blog/practical-react-query :) You will instantly benefit from background updates, (which you can further tweak to disable if you have local state already), and the loading spinner will not be shown if data already exists in the cache. I would suggest to use the |
Beta Was this translation helpful? Give feedback.
-
I have a similar question - new to GitHub Discussions and not sure what the etiquette is about creating a new thread vs commenting. Happy to move if needed. :) For easy of testing and Storybooking, I think I want to try separating container and presentational components. I have a presentational I don't have access to the state in the container component ( Similar to Ryan, I'd love to be able to show a loading indicate while It seems like I have 2 options:
Seems like either way I might need to track one additional piece of state, either to enable the query or to track loading status. Does that seem about right? Or am I missing something? I think I'm starting to get the "zen" of Small TypeScript excerpt to illustrate: const ConnectedSearch = () => {
// Option 1: Lift state here from child and useQuery(..., { enabled: isSearching })
const [filters, setFilters] = useState<Filters>({})
const queryClient = useQueryClient()
const search = async ({ query, sortBy }) => {
// Option 2: `setIsLoading(true)` here, then `setIsLoading(false)` after the query returns
//
// I wish this returned `{ isLoading, data }`, but maybe I'm missing something
// And maybe I really wish there was a `useLazyQuery` hook since I would need
// to use loading state outside this block anyway
const data = await queryClient.fetchQuery(
["users", { query, sortBy }],
() => api.getUsers({ query, sortBy })
)
}
return <SearchBar search={search} />
}
const SearchBar = ({ search }) => {
const [query, setQuery] = useState("")
const [sortBy, setSortBy] = useState<SortOrder>(SortOrder.availability)
const handleSubmit = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault()
search({ query, sortBy })
}
return <form onSubmit={handleSubmit}>...</form>
} |
Beta Was this translation helpful? Give feedback.
Cool. Incidentally, I have written about this exact use case in my blog: https://tkdodo.eu/blog/practical-react-query :)
The idea is to take the
data
returned fromuseQuery
and use it as a fallback to calculate the local state, which will take precedence as soon as the user has made some input. I've refactored your example to this approach: https://codesandbox.io/s/wandering-dust-yzpbf?file=/src/App.jsYou will instantly benefit from background updates, (which you can further tweak to disable if you have local state already), and the loading spinner will not be shown if data already exists in the cache.
I would suggest to use the
ReactQueryDevtools
- they help tremendously when usinguseQ…