RTK Query - Redux Toolkit
RTK Query - Redux Toolkit
How to set up and use Redux Toolkit's "RTK Query" data fetching functionality
PREREQUISITES
Introduction
Welcome to the Redux Toolkit Query tutorial! This tutorial will briefly introduce you to Redux
Toolkit's "RTK Query" data fetching capability and teach you how to start using it correctly.
INFO
For a more in-depth tutorial on RTK Query, see the full "Redux Essentials" tutorial on the
Redux core docs site.
RTK Query is an advanced data fetching and caching tool, designed to simplify common cases
for loading data in a web application. RTK Query itself is built on top of the Redux Toolkit core,
and leverages RTK's APIs like createSlice and createAsyncThunk to implement its
capabilities.
RTK Query is included in the @reduxjs/toolkit package as an additional addon. You are not
required to use the RTK Query APIs when you use Redux Toolkit, but we think many users will
benefit from RTK Query's data fetching and caching in their apps.
TypeScript JavaScript
src/services/pokemon.ts
With RTK Query, you usually define your entire API definition in one place. This is most likely
different from what you see with other libraries such as swr or react-query , and there are
several reasons for that. Our perspective is that it's much easier to keep track of how requests,
cache invalidation, and general app configuration behave when they're all in one central
location in comparison to having X number of custom hooks in different files throughout your
application.
TIP
https://redux-toolkit.js.org/tutorials/rtk-query 2/7
11/13/23, 4:24 PM RTK Query | Redux Toolkit
Typically, you should only have one API slice per base URL that your application needs to
communicate with. For example, if your site fetches data from both /api/posts and
/api/users , you would have a single API slice with /api/ as the base URL, and
separate endpoint definitions for posts and users . This allows you to effectively take
advantage of automated re-fetching by defining tag relationships across endpoints.
For maintainability purposes, you may wish to split up endpoint definitions across multiple
files, while still maintaining a single API slice which includes all of these endpoints. See
code splitting for how you can use the injectEndpoints property to inject API
endpoints from other files into a single API slice definition.
TypeScript JavaScript
src/store.ts
https://redux-toolkit.js.org/tutorials/rtk-query 3/7
11/13/23, 4:24 PM RTK Query | Redux Toolkit
TypeScript JavaScript
src/index.tsx
TypeScript JavaScript
src/App.tsx
https://redux-toolkit.js.org/tutorials/rtk-query 4/7
11/13/23, 4:24 PM RTK Query | Redux Toolkit
return (
<div className="App">
{error ? (
<>Oh no, there was an error</>
) : isLoading ? (
<>Loading...</>
) : data ? (
<>
<h3>{data.species.name}</h3>
<img src={data.sprites.front_shiny} alt={data.species.name}
/>
</>
) : null}
</div>
)
}
When making a request, you're able to track the state in several ways. You can always check
data , status , and error to determine the right UI to render. In addition, useQuery also
provides utility booleans like isLoading , isFetching , isSuccess , and isError for the
latest request.
Basic Example
https://redux-toolkit.js.org/tutorials/rtk-query 5/7
11/13/23, 4:24 PM RTK Query | Redux Toolkit
Okay, that's interesting... but what if you wanted to show multiple pokemon at the same time?
What happens if multiple components load the same pokemon?
Advanced example
RTK Query ensures that any component that subscribes to the same query will always use the
same data. RTK Query automatically de-dupes requests so you don't have to worry about
checking in-flight requests and performance optimizations on your end. Let's evaluate the
sandbox below - make sure to check the Network panel in your browser's dev tools. You will see
3 requests, even though there are 4 subscribed components - bulbasaur only makes one
request, and the loading state is synchronized between the two components. For fun, try
changing the value of the dropdown from Off to 1s to see this behavior continue when a
query is re-ran.
https://redux-toolkit.js.org/tutorials/rtk-query 6/7
11/13/23, 4:24 PM RTK Query | Redux Toolkit
https://redux-toolkit.js.org/tutorials/rtk-query 7/7