4.0.6 • Published 8 days ago

@drieam/react-query v4.0.6

Weekly downloads
-
License
MIT
Repository
-
Last release
8 days ago

@drieam/react-query

Convenient hooks and utilities for React Query.

Installation

Install @drieam/react-query and its peer dependency:

$ pnpm add @drieam/react-query react-query

Configuration

The library should be configured to the needs for your app, for Eduframe it would look like this:

import type { ReactNode } from 'react';
import { notification } from "antd";
import { Configuration } from "@drieam/react-query";
import { getCSRFToken } from "@common/dom";

const configuration = new Configuration({
  defaultHeaders: {
    "X-CSRF-Token": getCSRFToken(),
    "Cache-Control": "no-cache",
    Credentials: "same-origin",
  },
  isCamelCased: false,
  notifyError: (message: ReactNode) => notification.error({ message }),
  notifySuccess: (message: ReactNode) => notification.success({ message }),
});

Usage

Next to our configuration, we have to create a QueryClient instance and set that in our ReactQueryProvider. Afterwards we can use our useFetch, useFetchInfinite, useFetchPaginated, useCreate, useUpdate, and useDelete hooks.

Hereby a useFetch example based on React Query's documentation:

import { QueryClient, QueryClientProvider } from "react-query";
import { Configuration, useFetch } from "@drieam/react-query";

interface Label {
  id: number;
  name: string;
};

const configuration = new Configuration();
const queryClient = new QueryClient();

export default function App() {
  return (
    <ReactQueryProvider configuration={configuration} queryClient={queryClient}>
      {children}
    </ReactQueryProvider>
  );
}

function Example() {
  const { data, error, isLoading } = useFetch<Label[]>("/labels");

  if (isLoading) return "Loading...";

  if (error) return "An error has occurred: " + error.message;

  return (
    <div>
      <ul>
        {data.map((label: Label) => (
          <li key={label.id}>{label.name}</li>
        ))}
      </ul>
      <ReactQueryDevtools initialIsOpen />
    </div>
  );
}

Hooks and utilities

  • Query hooks
    • useFetch
    • useFetchInfinite
    • useFetchPaginated
    • useCreate
    • useUpdate
    • useDelete
  • Infinite query hooks
    • useInfiniteData
    • useInfiniteScroll
  • Utilities
    • isRequestError
  • Test utilities
    • makePaginated
4.0.6

8 days ago

4.0.5

9 days ago

4.0.4

15 days ago

4.0.3

15 days ago

4.0.2

5 months ago

4.0.1

10 months ago

4.0.0

1 year ago

3.2.0

2 years ago

3.1.3

2 years ago

3.1.2

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.0.0

2 years ago

2.0.0

2 years ago

1.1.0

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago