HOME

React Query

React Query is a data-fetching and server state management library for React applications. It abstracts the caching, background updates, and stale data out of your components, making these operations easier and more efficient. Here are some benefits:

  1. Data Fetching: React Query provides hooks like useQuery and useMutation that are used to fetch, cache, and update data in your React application. These hooks are declarative and automatically manage updates to the React component when the underlying data changes.

  2. Caching: React Query automatically caches data for you. When the same data is required again, it fetches from the cache instead of making an unnecessary network request. This makes your application more responsive and efficient.

  3. Background Updating: React Query supports background updating of data. This means that after the initial fetch, it periodically refetches the data

    to keep it up to date. This happens in the background and the UI is automatically updated when new data is available.

  4. Stale Data: React Query has built-in support to manage stale data. This means that when data is older or potentially outdated, React Query knows to fetch a fresh copy from your server.

  5. Devtools: React Query provides a set of devtools that can be enabled during development. This can be very helpful for seeing what's in the cache, what components are using what data, when data is updating, etc.

  6. Server State: React Query positions itself as the best solution for server state management. This is the data that's fetched from your server and is shared among various components in your application. It provides features for prefetching, caching, synchronizing and updating this server state.

  7. Mutation and Query Invalidation: Whenever data is mutated (like creating, updating, or deleting), React Query provides tools to easily invalidate and refetch queries that could be affected by these changes, ensuring your UI is always in sync with your server state.

Note that React Query does not replace a client state management library like Redux or MobX, but rather complements it by handling the server state, a part that traditionally has been a bit difficult to handle efficiently in React.


Setup
import {(QueryClient, QueryClientProvider)} from 'react-query'; 
const queryClient = new QueryClient();

function CustomApp({ Component, pageProps }: AppProps) {
  return (
  <>
    <QueryClientProvider client={queryClient}>
      <Component {...pageProps} />
    </QueryClientProvider>
  </>
  )
}

Query Example
import { useQuery } from 'react-query'
useGetDowntimeReaonsQuery: () => {
  const query = useQuery({
    queryKey: ['query-key-for-this-api'],
    queryFn: () => {
      return fetchWrapper(url, {
        method: 'GET'
      });
    }
  });
  return query;
},

const { data } = useGetDowntimeReaonsQuery();

Mutation Example
useNotifyDowntimeSettingsMutation: () => {
  const mutation = useMutation({
    mutationFn: (data) => {
      return fetchWrapper(url, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Data-Type': 'json'
        },
        body: JSON.stringify(data)
      });
    }
  });
  return mutation;
},

useNotifyDowntimeSettingsMutation.mutate(formData, {
  onSuccess: (data) => {
    // do something
  }
});