From the course: Build Modern Web Apps with React, Hooks, State Management, and APIs Using Vite or Next.js

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Fetching all posts

Fetching all posts

Hello and welcome back. Now let's discuss how we can fetch all the posts using 10 stack query and the project setup we have. We are going to create a hook called usePosts.gs which is going to be ts in our case because we are using TypeScript. This hook is going to call the useQuery hook from 10 stack query. We are going to pass on a query key, which is going to be post in our case. Then we have the query function. In this query function, we usually write down a API call like this. This has to be an async function, which is going to call the get API and return back the data. Since we have set up our project in a more advanced way, then we are going to call the get old post. We are going to call the getAllPost method from the blogAPI.ts file. Let's continue and see this in action. Similarly, we are going to need a component which is going to call the hook that we have created and this hook is going to be called and it's going to return a data error and loading states. Then we simply…

Contents