By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
World of SoftwareWorld of SoftwareWorld of Software
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Search
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
Reading: Are React and RTK Query a New Easy Way for Redux? | HackerNoon
Share
Sign In
Notification Show More
Font ResizerAa
World of SoftwareWorld of Software
Font ResizerAa
  • Software
  • Mobile
  • Computing
  • Gadget
  • Gaming
  • Videos
Search
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Have an existing account? Sign In
Follow US
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
World of Software > Computing > Are React and RTK Query a New Easy Way for Redux? | HackerNoon
Computing

Are React and RTK Query a New Easy Way for Redux? | HackerNoon

News Room
Last updated: 2025/04/10 at 9:13 PM
News Room Published 10 April 2025
Share
SHARE

First of all, let’s remember what the classic request and data storage looks like with redux-saga and @reduxjs/toolkit.

// slice
// We use an enum with statuses to easily manipulate them in the future.
enum EOperationStatus {
  initial = "initial",
  pending = "pending",
  error = "error",
  success = "success",
}

const initialState: IInitialState = {
  items: [],
  itemsStatus: EOperationStatus.initial,
};

export const itemsSlice = createSlice({
  name: "items",
  initialState,
  reducers: {
    getItems: state => {
      state.itemsStatus = EOperationStatus.pending;
    },
    setItems: (state, {payload}: PayloadAction<IInitialState["items"]>) => {
      state.items = payload;
    },
    setItemsStatus: (
      state, { payload}: PayloadAction<EOperationStatus>,
    ) => {
      state.itemsStatus = payload;
    },
    ...

// api
const getItems = async () => await HttpHandler.get<IItem[]>("/items");
export const API = {
  getItems, 
};

// saga
function* getItemsSaga() {
  try {
    const res = yield* call(API.getItems);
    yield* put(itemsSlice.setItems(res));
    // На простых запросах нам постоянно нужно будет писать одно и тоже:
    // сохраняем результат и обновляем статусы.
    yield* put(itemsSlice.setItemsStatus(EOperationStatus.success));
  } catch (e) {
    yield* put(itemsSlice.setItemsStatus(EOperationStatus.error));
  }
}

// selector
export const itemsSelector = selector(s => s.items);
export const itemsStatusSelector = selector(s => s.itemsStatus);

// Component.tsx
export const Component = () => {
  const items = useSelector(itemsSelector);
  const itemsStatus = useSelector(itemsStatusSelector);
  
  if(itemsStatus === EOperationStatus.initial || 
     itemsStatus === EOperationStatus.pending){
       return 'loading';
     }
     return <>{items.map(el => <div key={el.id}>{el.id}</div>)}</>
}

This is what the standard request template looks like with manipulation of statuses and data display. It is versatile as a Swiss knife and can be completely customized. Using this template, we can perform requests of any complexity and control all the details.

But what if the requests are as easy as in the example above: we just take the data and show it. Do we really need to do this routine every time if we can make our lives easier?

Just for such cases, there is an rtk query tool (included in the reduxjs/toolkit). Let’s see what our request looks like now.

// rtk
interface IItem {
  id: number;
}

const apiWithTags = rtkApi.enhanceEndpoints({});

export const itemsRtkApi = apiWithTags.injectEndpoints({
  endpoints: (build) => ({
    getItems: build.query<
    IItem[],
    undefined>({
      query: () => ({
        url: `/items?q=true`,
      }),
      async onQueryStarted(_, { queryFulfilled }) {
        try {
          await queryFulfilled;
        } catch (_e) {
          noop();
        }
      },
      keepUnusedDataFor: 0,
    }),
  }),
});

// Component
...
  const { data, isError, isFetching, isLoading, status,
   error, refetch } = itemsRtkApi.useGetItemsQuery(
    undefined,
    {refetchOnMountOrArgChange: true}
  );
...

In build.query<>, we pass two arguments, 1 — the type that will be returned and 2 — the parameters that we accept in the hook, rtk parameter.

Next, in the component, we call the hook and pass the data to call the api and the second argument, the parameters for the hook itself. For example, the refetchOnMountOrArgChange property will re-request data when the component is unmounted. You can read more about each of them in the documentation.

But most importantly, in hook’s response, we get all the statuses/data/other features. Each of them can be useful for a specific situation, but together they cover almost all cases.

// Component.tsx
export const Component = () => {
  const { data: items, isError, isFetching } = itemsRtkApi.useGetItemsQuery(
    undefined,
    {refetchOnMountOrArgChange: true}
  );
  
  if(isFetching){
     return 'loading';
   }
  if(isError){
     return 'isError';
   }
   return <>{items.map(el => <div key={el.id}>{el.id}</div>)}</>
}

Now the component looks light and elegant. Here we use isFetching (the difference from isLoading is that it is activated at any load, and isLoading only at the initial one). You can call refetch to re-query or check for isError in case of an error.

We can also trigger a delayed hook. The so-called lazy query.

// Component.tsx
export const Component = () => {
  const [fetchItems, 
  {isFetching, {isError, data: items, isUninitialized}] = itemsRtkApi.useLazyGetItemsQuery();
  
  useEffect(() => {
    fetchItems()
  }, [])
  
  if(isFetching || isUninitialized){
     return 'loading';
   }
  if(isError){
     return 'isError';
   }
   return <>{items.map(el => <div key={el.id}>{el.id}</div>)}</>
}

Here we make a request from the hook. It is usually necessary if we want to pass an argument to a hook that does not appear immediately. We also check isUninitialized that the hook has been initialized and then it will become false when loading.

Plus, rtk makes it possible to use mutations to update data. It will look something like this:

// rtk
...
updateItems: build.mutation<IItem[], { param: number }>({
  queryFn: async ({ param }, api, _extraOptions, baseQuery) => {
    const { data, error } = (await baseQuery(`/items/?param=${param}`)) as QueryReturnValue<
      IItem[],
      FetchBaseQueryError
    >;

    if (error) {
      return { error };
    }

    api.dispatch(
      itemsRtkApi.util.updateQueryData('getItems', undefined, (draft) => {
        return Object.assign(draft, data, [...(draft|| []), ...(data|| [])] });
      })
    );

    return { data };
  },
}),
...

What is rtk not suitable for?

For complex operations like pagination or storing variables between requests. For such cases, it is better to use sagas. After all, its main goal is to save us from routine requests.

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Twitter Email Print
Share
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Scientists may have uncovered a way to stop humans from needing sleep to dream
Next Article Ericsson unveils AI-led enterprise wireless-first branch architecture | Computer Weekly
Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Stay Connected

248.1k Like
69.1k Follow
134k Pin
54.3k Follow

Latest News

Gates on Musk: 'World’s richest man killing the world’s poorest children'
News
iFlytek chair delivers confident speech to employees despite projected net profit fall of over 70% for 2023 · TechNode
Computing
AI-generated video gave victim a voice at his killer’s sentencing in Arizona
News
Narwal Freo Pro robot drops to a new all-time low price!
News

You Might also Like

Computing

iFlytek chair delivers confident speech to employees despite projected net profit fall of over 70% for 2023 · TechNode

3 Min Read
Computing

Deadzone: Rogue Rockets To Steam’s Top 10 Global Sellers With 100K+ Players In Week 1 | HackerNoon

3 Min Read
Computing

Steam Deck Adds Battery Maximum Charge Limit Control In Newest Beta

1 Min Read
Computing

Alibaba reports slower growth in core businesses amid rising competition · TechNode

1 Min Read
//

World of Software is your one-stop website for the latest tech news and updates, follow us now to get the news that matters to you.

Quick Link

  • Privacy Policy
  • Terms of use
  • Advertise
  • Contact

Topics

  • Computing
  • Software
  • Press Release
  • Trending

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

World of SoftwareWorld of Software
Follow US
Copyright © All Rights Reserved. World of Software.
Welcome Back!

Sign in to your account

Lost your password?