Skip to main content
Practice Problems

What is RTK Query in Redux Toolkit?

javascript
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react"; export const api = createApi({ baseQuery: fetchBaseQuery({ baseUrl: "/api" }), endpoints: (builder) => ({ getUser: builder.query({ query: (id) => `users/${id}`, }), updateUser: builder.mutation({ query: ({ id, ...data }) => ({ url: `users/${id}`, method: "PUT", body: data, }), }), }), }); // Auto-generated hooks export const { useGetUserQuery, useUpdateUserMutation } = api; // Usage function UserProfile({ id }) { const { data, isLoading } = useGetUserQuery(id); const [updateUser] = useUpdateUserMutation(); if (isLoading) return <div>Loading...</div>; return <div>{data.name}</div>; }

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.

Finished reading?
Practice Problems