React-query - How Can I Access My Queries In Multiple Components?
I'm just beginning with react query and used it to get a list of books from the server using useQuery in my ListBooks Component const { data, error, isLoading, isError } = useQuer
Solution 1:
react-query
manages query caching based on query keys, so in the other component, as long as they're all wrapped inside QueryClientProvider
, you just need to call useQuery
with the matching key (books
), and react-query
will return the appropriate data for you.
If you use the same query in multiple places, consider adding a wrapper hook to keep your code clean:
functionuseAllBooks() {
returnuseQuery("books", getAllBooks)
}
functionComponent1() {
const {...} = useAllBooks()
return (...)
}
functionComponent2() {
const {...} = useAllBooks()
return (...)
}
Post a Comment for "React-query - How Can I Access My Queries In Multiple Components?"