Repository navigation
Replies: 1 comment
|
For the “two requests in parallel, then a third, publish everything together” case, I'd use one client-side RTK Query endpoint as the unit of work. It doesn't require creating a new HTTP endpoint on your server, and There isn't a transaction that couples the result updates of two independent import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
// Payloads are opaque here; use your actual response types/validation.
type Bundle = { first: unknown; second: unknown; third: unknown };
export const api = createApi({
baseQuery: fetchBaseQuery({ baseUrl: 'https://example.test/api/' }),
endpoints: (build) => ({
getBundle: build.query<Bundle, string>({
async queryFn(id, _api, _extra, fetchWithBQ) {
const [first, second] = await Promise.all([
fetchWithBQ(`first/${id}`),
fetchWithBQ(`second/${id}`),
]);
if (first.error) return { error: first.error };
if (second.error) return { error: second.error };
const third = await fetchWithBQ({
url: 'third',
method: 'POST',
body: { first: first.data, second: second.data },
});
if (third.error) return { error: third.error };
return {
data: { first: first.data, second: second.data, third: third.data },
};
},
}),
}),
});Then use For two backends, The tradeoff is caching: direct This gives atomic client publication, not a transactional server snapshot across services. If the payloads must represent exactly the same server revision, the APIs need to provide that guarantee. Verified with RTK 2.13.0/React 19.3.1: parallel/dependent completion, retaining old data during argument changes and failures, rapid argument changes with a late old result, and same-argument refetching. The docs include multiple requests in one |
Uh oh!
There was an error while loading. Please reload this page.
Does RTKQ offer any kind of mechanism for coupling the results of one query hook with another?
When you do:
Whenever
someIdchanges there will be a period where the component contains the old data from one hook and the new data from the other hook causing an inconsistent UI state.So far I came up with this:
currentDatafor both, but this makes the UI very jumpy. The nice thing about working with hooks is that thedataremains unchanged until the query is actually finished.However this feels like such a common usecase that I feel like I might be overlooking a better way to do this.
And in the above example its just two simultaneous requests. What if you more complex requirements such as two simultaneous requests followed by another request and you want all of the data to be made available in the UI at once while still showing the old data in the meanwhile.
All reactions