Logo

dev-resources.site

for different kinds of informations.

Manual Fetch with Tanstack Query

Published at
12/21/2023
Categories
tanstackquery
reactquery
react
api
Author
ldsrogan
Author
8 person written this
ldsrogan
open
Manual Fetch with Tanstack Query

TanStack Query (FKA React Query) is often described as the missing data-fetching library for web applications, but in more technical terms, it makes fetching, caching, synchronizing and updating server state in your web applications a breeze. - TanStack Query Overview

As a developer, TanStak Query is very efficient tool that handle all our needs when we fetch the data through APIs.

Very often we want to fetch the query manually not from useEffect or automatic fetching when entering the page.

To achieve this manual query, we can use QueryClient.

As all of you know, we already define the QueryClient to use the TanStack Query in our service.

import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

const queryClient = new QueryClient();

function App() {
  return <QueryClientProvider client={queryClient}>
    <Main />
  </QueryClientProvider>

}
Enter fullscreen mode Exit fullscreen mode

If you want to use the queryClient that you define above, you can simply use the hook called useQueryClient in your code.

import { useQueryClient } from '@tanstack/react-query';

export default function Main() {
  const queryClient = useQueryClient();
  ...
}
Enter fullscreen mode Exit fullscreen mode

And then, you can use the function called fetchQuery. fetchQuery works exactly like useQuery, so we can add queryKey, queryFn, and other options that useQuery uses. (See the available options from the official documentation.

Here is an example.


const handleClick = async () => {
  try {
    // data will be the response of the fetch
    const data = await queryClient.fetchQuery({
        queryKey: ['fetch-data'],
        queryFn: functionForFetch(),
      });
  } catch (e){
    // handling error...
  }
};

<button onClick={handleClick}>Fetch</button>

Enter fullscreen mode Exit fullscreen mode
reactquery Article's
30 articles in total
Favicon
Create an SSR Application with Vite, React, React Query and React Router
Favicon
TanStack query or RTK query.
Favicon
Simplified State Management with useRQState: A Better Alternative to useState
Favicon
Master React API Management with TanStack React Query: Best Practices & Examples
Favicon
How to Fetch Data Using Axios and React Query in ReactJS
Favicon
Efficient Refresh Token Implementation with React Query and Axios
Favicon
react-query swrjs alova In-Depth Comparison
Favicon
Mastering React Query. Simplifying Data Management in React with Separation Patterns
Favicon
Mastering React Query. Structuring Your Code for Scalability and Reusability
Favicon
Why you should try React Query?
Favicon
How to used Suspense?
Favicon
Infinite list loading πŸ€”, with React Query - useInfiniteQuery hook !
Favicon
Building a CRUD app with React Query, TypeScript, and Axios
Favicon
TLDR; Suspense in react-query
Favicon
TLDR; gcTime & staleTime in react-query
Favicon
Building Infinite Scrolling in React Js with React Query v5
Favicon
Optimized Infinite Scroll with Next.js 14 Server Actions and React Query
Favicon
Building a Todo List with TypeScript and React Query: A Comprehensive Guide
Favicon
React Safe Query - A lightweight, type-safe wrapper built around React Query
Favicon
React Query Mutations Offline React-Native
Favicon
Improving UX by using cache
Favicon
How to organize your API layer in React using React Query
Favicon
Conociendo a React Query
Favicon
Build a CRUD App in React with Tanstack Query and Material UI [Final]
Favicon
Build a CRUD App in React with Tanstack Query and Material UI [Part 2]
Favicon
Integrating React Query with Next.js πŸš€
Favicon
Build a CRUD App in React with Tanstack Query and Material UI [Part 1]
Favicon
React query for data streaming
Favicon
Manual Fetch with Tanstack Query
Favicon
Setting up React Query in your ReactΒ project

Featured ones: