0.0.29 • Published 8 months ago

@kontsedal/light-query v0.0.29

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

light-query

Small (1.7kb gzipped) alternative to the react-query library. It's a simple set of react hooks that allows you to fetch data from a server and have a control of retries and caching.

Installation

npm i @kontsedal/light-query

Usage

useQuery

Fetches and caches the result of a function call, so consecutive calls will return the same result without making a request to the server.

import { useQuery } from "@kontsedal/light-query";
const { data, error, isLoading, refetch, reset } = useQuery("user", () => fetch("/user"), {
  refetchInterval: () => 5000
});

Result object

  • data - data returned by the function
  • error - error returned by the function
  • isLoading - boolean flag that indicates if the function is currently fetching data
  • isIdle - boolean flag that indicates that the fetch function has not been called yet
  • isUpdating - boolean flag that indicates that the fetch function is currently updating the data that was previously fetched
  • lastFetchedAt - timestamp of the last fetch (successful or failed)
  • refetch - function that forces the refetch of the data
  • reset - function that removes the data from the cache

Options object

  • refetchInterval - function that returns the interval in milliseconds to refetch the data. If the function returns non positive number, the data will not be refetched. Function is called with the last fetched data as an argument.
  • cacheTime - the time in milliseconds to keep the data in the cache after it is no longer used. If the data is not used for this time, it will be removed from the cache.
  • staleTime - the time in milliseconds to consider the data as fresh and not refresh it on the next access. If the data is older than this time, it will be refetched on the next access.
  • refetchOnWindowFocus - boolean flag that indicates if the data should be refetched when the window is focused again
  • refetchOnReconnect - boolean flag that indicates if the data should be refetched when the network connection is reestablished
  • enabled - boolean flag that indicates if the data should be fetched. If set to false, the data will not be fetched and the cache will not be updated. If there was no data in cache, this query will have an isIdle flag set to true.
  • retry - function that is called when the fetch function fails. Three arguments are passed to this function: number of attempt, error and the latest data. The function should return the time in milliseconds to wait before the next attempt. If it returns not positive number, it won't retry anymore.

useMutation

The simple wrapper around the function to provide you with isLoading, error and mutate function.

import { useMutation } from "@kontsedal/light-query";

const { isLoading, error, mutate } = useMutation(async (login: string, password: string) => {
  try {
    const response = await fetch("/login", {
      method: "POST",
      body: JSON.stringify({ login, password })
    });
    return response.json();
  } catch (error) {
    throw new Error("Failed to login");
  }
})

Result object

  • isLoading - boolean flag that indicates if the function is currently executing
  • error - error returned by the function
  • mutate - function that executes the mutation
0.0.28

12 months ago

0.0.29

8 months ago

0.0.26

2 years ago

0.0.27

2 years ago

0.0.24

2 years ago

0.0.25

2 years ago

0.0.21

2 years ago

0.0.22

2 years ago

0.0.23

2 years ago

0.0.20

2 years ago

0.0.14

2 years ago

0.0.15

2 years ago

0.0.16

2 years ago

0.0.17

2 years ago

0.0.18

2 years ago

0.0.19

2 years ago

0.0.11

2 years ago

0.0.12

2 years ago

0.0.13

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.5

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago