Replies: 3 comments 3 replies
-
for both cases i would look into raising the query into a context const PostsProvider = () => (
<PostsContext.Provider value={useInfiniteQuery(...)}>
<PostsSideEffects />
<Children />
</PostsContext.Provider>
)
const PostsSideEffects = () => {
const posts = usePosts()
React.useEffect(() => {
if (posts.status === whatever) {
doTheThing()
}
}, [posts.status])
return null
} |
Beta Was this translation helpful? Give feedback.
-
I had to find a solution for something similar. I've seen a few discussions in here regarding using the Like @wolverineks suggests, I ended up using React Context and it's been alright. Here is a real basic example that I cooked up to get something working https://codesandbox.io/s/subscribe-70q5e It has a "deeply nested child" that is creating an All the magic happens in the import React from "react";
import { useQuery, useMutation, queryCache } from "react-query";
import * as api from "./distractions/api";
function useItemsFromAPI() {
const { data: items, status: findStatus, isFetching } = useQuery(
["items"],
api.fetchItems
);
const [createItem, { status: createStatus }] = useMutation(api.createItem, {
onSuccess: (item) =>
queryCache.setQueryData("items", (previousItems) => [
...previousItems,
item
])
});
return { items, findStatus, createStatus, isFetching, createItem };
}
const ItemContext = React.createContext();
function ItemProvider({ children }) {
return (
<ItemContext.Provider value={useItemsFromAPI()}>
{children}
</ItemContext.Provider>
);
}
function useItems() {
const context = React.useContext(ItemContext);
if (context === undefined) {
throw new Error("useItems must be used within ItemProvider");
}
return context;
}
export { useItems, ItemProvider }; but the Finally, the app I am working on is a lot more complex then my above sandbox. It has to leverage using https://kentcdodds.com/blog/how-to-use-react-context-effectively |
Beta Was this translation helpful? Give feedback.
-
For my use case I get message add/remove "events", yet a client just wants to see a list of messages. I use a "messages" query that mounts the events query and then updates its cache as events come during the rendering phase. The messages query function does nothing. This neatly takes care of the observer pattern and feels semantically correct. The observers of the messages query have no idea about the underlying architecture, which is great. I would like to see explicit support for these types of "dependent" queries. All I need is a way to omit the query function. Right now if you do that you get a warning message. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey Tanner,
Suppose I have one
useInfiniteQuery
hook, and I want to display the resulting data in multiple sibling components. Or in a different case, my component is rendering a child component which fetches data viauseQuery
, but the parent needs to monitor the child'sstatus
What I need is a way to read a query from the cache in a reactive way. This could be done by making
fetchFn
optional, and allowing a component to subscribe to a query without having to provide the fetcher for it.Currently I'm using this as a first attempt:
Doe you see any concerns with this? Is this something the library could offer by default?
Beta Was this translation helpful? Give feedback.
All reactions