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

react-query-toolkit

v0.1.1

Published

[![codecov](https://codecov.io/gh/lifeisegg123/react-query-toolkit/branch/main/graph/badge.svg?token=YQ37N8E2R3)](https://codecov.io/gh/lifeisegg123/react-query-toolkit) > note: This library is in WIP stage. I would not recommend using it in production.

Downloads

2

Readme

react-query-toolkit

codecov

note: This library is in WIP stage. I would not recommend using it in production.

Introduction

This library helps you to use React-Query much easier. It provides two functions. createQueryToolkit for queries, createMutationToolkit for mutations. These functions allows you to less concern about how to manage query key or api functions, and prevent you from writing same code(eg. give same options to useQuery & prefetchQuery) for queries or mutations.

Any type of contribution(pr, issue, etc...) is welcome.

Installation

# with npm
npm i react-query-toolkit

# with yarn
yarn add react-query-toolkit

# with pnpm
pnpm add react-query-toolkit

Usage

Before you start, make sure you have installed React-Query.

Create toolkit

// queryClient.js
import { QueryClient } from 'react-query'
import { createQueryToolkit, createMutationToolkit } from 'react-query-toolkit';

export const queryClient = new QueryClient();

export const queryToolkit = createQueryToolkit(queryClient);

export const mutationToolkit = createMutationToolkit(queryToolkit)

createQueryToolkit and createMutationToolkit require queryClient instance as a parameter.

Create query and mutation

// example.js
import { queryToolkit, mutationToolkit } from './queryClient';

export const exampleQuery = queryToolkit(['exampleQuery'], mockQueryApi);

export const exampleMutation = mutationToolkit(['exampleMutation'], mockMutationApi);

By making query and mutation like this, it will handle all react-query api with same key and api function (useQuery, useIsFetching, prefetchQuery, getQueryData, etc...).

Basic usage

// ExampleComponent.js
import {exampleQuery, exampleMutation} from './example';

function ExampleComponent () {
  const { data, error, isLoading } = exampleQuery.useQuery();
  const { isLoading: isMutationLoading, mutate } = exampleMutation.useMutation();

  return (
    <div>
      <div>{isLoading ? 'Loading...' : data}</div>
      <button onClick={() => mutate()}>
        {isMutationLoading ? 'Loading...' : 'Click me'}
      </button>
    </div>
  );
}

note: useQuery takes arguments of api function as an array.
if you need to pass additional options to useQuery without any arguments, pass an empty array.

Use query from other component

// AnotherExampleComponent.js
import {exampleQuery, exampleMutation} from './example';

function AnotherExampleComponent () {
  const isFetching = exampleQuery.useIsFetching();
  const isMutating = exampleMutation.useIsMutating();
  if(isFetching || isMutating) {
    return <div>Loading...</div>
  }

  return (
    <div>
      {/* return something here */}
    </div>
  );
}

API Reference

createQueryToolkit

parameters

  • queryClient: QueryClient
    • Required
    • queryClient instance

returns

  • createQuery: (queryKey, queryFn, options) => QueryToolkit
    • returns a function that will return a instance of QueryToolkit

createQuery

parameters

  • queryKey: QueryKey

    • Required
    • query's key(needs to be Array)
  • queryFn: (...args: TQueryFnArgs[]) => (context: QueryFunctionContext) => Promise<TData> | TData

    • Required
    • query's api function
    • args needs to be given by parameter of useQuery, fetchQuery, prefetchQuery, etc...
  • options: { passArgsToQueryKey: boolean, queryType: 'query' | 'infiniteQuery' }

    • Optional
    • passArgsToQueryKey: boolean
      • Defaults to true
      • if set to true, queries arguments will be passed to queryKey.
    • queryType: 'query' | 'infiniteQuery'
      • Defaults to 'query'
      • if set to 'query', QueryToolkit will have useQuery, fetchQuery, prefetchQuery methods.
      • if set to 'infiniteQuery', QueryToolkit will have useInfiniteQuery, fetchInfiniteQuery, prefetchInfiniteQuery methods.
    • defaultOptions: QueryOptions & UseQueryOptions & UseInfiniteQueryOptions

returns

  • QueryToolkit
    • will Return a instance of QueryToolkit

createMutationToolkit

parameters

  • queryClient: QueryClient
    • Required
    • queryClient instance

returns

  • createMutation: (queryKey, queryFn, options) => MutationToolkit
    • returns a function that will return a instance of MutationToolkit

createMutation

parameters

  • mutationKey: MutationKey

    • Required
    • mutation's key(needs to be Array)
  • mutationFn: (variables: TVariables) => Promise<TData>

    • Required
    • mutation's api function
  • defaultOptions: UseMutationOptions

returns

  • MutationToolkit
    • return a instance of MutationToolkit