Dung (Donny) Nguyen

Senior Software Engineer

Auto-generated React Hooks

RTK Query automatically generates React hooks for each endpoint you define in your API slice. This feature greatly simplifies data fetching and state management in your React components. Instead of manually writing logic to handle loading states, errors, and data, you just use the generated hook, and RTK Query handles all the boilerplate.

How they’re generated 🛠️

The hooks are generated based on the name of the endpoint you define. For a query endpoint named getPosts, RTK Query creates a hook called useGetPostsQuery. Similarly, for a mutation endpoint like addPost, it creates a hook called useAddPostMutation.

You must use the React-specific entry point for createApi to get these hooks: import { createApi } from '@reduxjs/toolkit/query/react'

Primary Hooks

RTK Query generates several types of hooks, but a few are the most commonly used:

Using the Hooks

You typically import the generated hooks from your API slice definition file. They provide a result object with a variety of useful properties, including:

By using these hooks, you don’t have to manually manage loading and error state in your components. The hooks handle it all for you.