npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kontsedal/light-query

v0.0.28

Published

A lightweight and dumb react-query alternative

Downloads

78

Readme

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