What is RTK Query?

Answer

RTK Query is a powerful data fetching and caching tool built into Redux Toolkit — RTK's answer to React Query. Define an API: import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react"; const api = createApi({ reducerPath: "api", baseQuery: fetchBaseQuery({ baseUrl: "/api" }), endpoints: (builder) => ({ getUser: builder.query({ query: (id) => \`/users/\${id}\` }), createUser: builder.mutation({ query: (body) => ({ url: "/users", method: "POST", body }), invalidatesTags: ["Users"] }) }) });. Generated hooks: const { data, isLoading } = useGetUserQuery(id); const [createUser] = useCreateUserMutation();. Features: automatic caching, background refetching, cache invalidation with tags, optimistic updates, pagination. Key advantage over React Query: since RTK Query integrates with the Redux store, all cached data is visible in Redux DevTools and can be accessed by regular Redux selectors. For teams already using Redux, RTK Query is the natural choice for server state. For teams not using Redux, React Query (TanStack Query) is simpler to set up.