Repository navigation
Replies: 2 comments 4 replies
4 replies
|
RTK Query and manual Option 1: Keep both, migrate incrementally Add Option 2: Wrap legacy calls inside RTK Query const api = createApi({
baseQuery: fetchBaseQuery({ baseUrl: "/api" }),
endpoints: (builder) => ({
// new endpoint using RTK Query
getUser: builder.query<User, string>({
query: (id) => `/users/${id}`,
}),
// legacy endpoint wrapped
legacyData: builder.query<Data, void>({
queryFn: async () => {
const res = await existingFetchFn(); // your existing logic
return { data: res };
},
}),
}),
});Option 3: Share auth headers across both If both approaches need auth, set once in const baseQuery = fetchBaseQuery({
baseUrl: "/api",
prepareHeaders: (headers, { getState }) => {
const token = (getState() as RootState).auth.token;
if (token) headers.set("Authorization", `Bearer ${token}`);
return headers;
},
});Then for existing fetch calls outside RTK Query, read the same token from the store: const token = store.getState().auth.token;Migrate endpoints one at a time. RTK Query handles caching, loading states, and invalidation — so endpoints that benefit most from those features are good migration candidates. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I’m migrating an app to Redux Toolkit Query but still need some existing REST calls outside RTK Query. What’s the best way to integrate both approaches smoothly?
All reactions