blog/react-query-and-react-context #97
Replies: 15 comments 9 replies
-
Thanks for always keeping us updated on how to make good decisions when using React Query. |
Beta Was this translation helpful? Give feedback.
-
So cool as always ! Thx |
Beta Was this translation helpful? Give feedback.
-
Thanks for another great article! I think Context is also a good solution when we have multiple Like in the example below, TQ cannot deduplicate network fetches because Child components are conditionally rendered
But with Context, we can avoid having duplicate network requests + props drilling, and Typescript will be pleased. The only downside I can think of is, if we have Just wondering in this case, do you think if Context is still a better approach OR just setting a |
Beta Was this translation helpful? Give feedback.
-
Good write up (as usual). Hard to think of any other use case than authentication or RBAC. The “drawback” becomes a feature in these instances: Do other examples come to mind? |
Beta Was this translation helpful? Give feedback.
-
I found a Typo in Code Example under the Heading Being self-contained. export const useUserQuery = (id: number) => {
return useQuery({
queryKey: ['user', id],
queryFn: () => fetchUserById(id),
})
}
export const useCurrentUserQuery = () => {
const id = useCurrentUserId()
return useUserQuery(id) // <-- Here's the Fix
} |
Beta Was this translation helpful? Give feedback.
-
Thanks for your great article! 👏 |
Beta Was this translation helpful? Give feedback.
-
Great advice, thanks a lot for sharing this Tk!! 😁😁 |
Beta Was this translation helpful? Give feedback.
-
can't wait for v5! |
Beta Was this translation helpful? Give feedback.
-
Nice. Glad it's not just me. I've been using it with react-router's outlet context and nested routes, works decent, e.g: function Routes() {
return (
<Routes>
<Route match="/:id" component={ResourceLayout}>
<Route match="/edit" ... />
</Route>
</Routes>
)
}
function ResourceLayout() {
const id = useParams();
const resourceQuery = useResourceQuery(id);
if (resourceQuery.data) {
return <Outlet context={resourceQuery.data}
}
// [...]
}
function useResourceContext() {
const context = useOutletContext<ResourceContext>();
invariant(/* ... */);
return context;
} |
Beta Was this translation helpful? Give feedback.
-
Nice! I also use React Context as a DI mechanism for React Query. Instead of injecting the data though I inject a service implementation. Makes it easy to swap everything out with mocks as needed. |
Beta Was this translation helpful? Give feedback.
-
thanks for great article |
Beta Was this translation helpful? Give feedback.
-
Great! |
Beta Was this translation helpful? Give feedback.
-
Thanks for this, it's really amazing and I'm glad that is a tip since I've been using this to get application settings and I always wonder if it's a bit out of the "rule", I have a question tho, since I found myself doing this when testing components that are wrapped in this provider and bc the provider is in charge of the query, it returns a loader, an error or the children depending on the query status I've always wait until the loader component is not in the screen anymore but I feel it's a repetitive solution, what do you think would be the best approach to test components that are children of this component to avoid waiting for the loader to disappear? I was thinking to provide a customQueryHook that always returns false to |
Beta Was this translation helpful? Give feedback.
-
Hi Dominik. Top-notch content as always! Just curious, should we check if the data was successfully fetched before passing it as a value? TypeScript still infers it as possibly undefined:
So, would it be a correct approach to do something like this?
|
Beta Was this translation helpful? Give feedback.
-
Thanks for documenting this pattern! It was really helpful when setting up my app since it allowed me to have a top level loading state while ensuring data was defined in each component. I was wondering if there might be a way to use this context approach while also reaping some of the benefits of tracked queries? I'm struggling to think of a way to do so. As an example, let's say the data I am fetching is an object with 20 keys. Each of my components uses 1-3 keys from the data object. Right now I'm passing the entire object through context after ensuring there is no loading or error state, so when I update the data, every component re-renders rather than just the components that use the changed fields. I'd imagine I should either not worry about optimizing renders in this case or somehow split up the context, but was curious if there might still be a way to take advantage of some of the tools react query provides out of the box to assist with such optimizations. |
Beta Was this translation helpful? Give feedback.
-
blog/react-query-and-react-context
Can it make sense to combine React Query with React Context ? Yes, sometimes ...
https://tkdodo.eu/blog/react-query-and-react-context#inject-comments
Beta Was this translation helpful? Give feedback.
All reactions