How to use useQuery in React Queries

Asked 1 years ago, Updated 1 years ago, 358 views

As far as I know, the data obtained by calling the API with useQuery is turned into a map to spray it on the screen.

export const getList = async () => {
  const { data } = await api.get("/~/~/~/list");
  return data;
};
export const getNewList = async () => {
  const { data } = await api.get("/~/~/~/new-list");
  return data;
};

If there are these two things to bring, how should I use useQuery?

react

2023-02-28 20:27

1 Answers

The useQuery does not import only one api.
It's convenient to think of it as a function to get the result of executing an asynchronous function into a hook.

const {
    data, // returned to array by code below. If it fails, it is undefined.
} } = useQuery(
    ['getList', 'getNewList'],
    async () => {
        // The logic inside this function can be written as desired.
        const list = await getList();
        const newList = await getNewList();
        // You can also do the return method as you wish.
        return [list, newList];
    },
);

Make a habit of reading official documents!


2023-02-28 21:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.