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-keys

v1.0.3

Published

helps create unique query keys for use with the react-query package

Downloads

18

Readme

npm version license types commits

react-query-keys

helps create unique query keys for use with the react-query package. inspired by the query-key-factory package but less extensive and therefore simpler to use.

Prerequisites

The purpose of this package is to make it easier to configure query keys for use with React Query library. You don't need that to use this package, but it is pretty much useless without it

$ yarn add @tanstack/react-query

Table of contents

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Installation

To install the library, run:

$ npm install react-query-keys

Or if you prefer using Yarn:

$ yarn add react-query-keys

Usage

Creating your keys

import ReactQueryKeys from "react-query-keys";

// the name of this section of the app
const keysName = "customers";

const queryKeyConfig = {
  keyDefinitions: {
    list: {
      dynamicVariableNames: ["productCount", "customerId"],
    },
    detail: {
      dynamicVariableNames: ["customerId"],
    },
    pagedList: {
      childOf: "list",
      dynamicVariableNames: ["skip", "take"],
    },
  },
};

// create an instance of your keys for this section. I like to do this once for each section of an app and split my queries accordingly
const customerQueryKeys = new ReactQueryKeys(keysName, queryKeyConfig);

Using your keys

import { useQuery, useMutation } from "@tanstack/react-query";

const useGetCustomersQuery = (customerId: string) =>
  useQuery(
    customerQueryKeys.key("list", { customerId }), // resolves to ['customers', 'list', { customerId }]
    () => getCustomers(customerId)
  );

const useCreateCustomerQuery = () =>
  useMutation(
    ({ customerId, customer }: { customerId: string; customer: Customer }) =>
      createCustomer(customer),
    {
      // invalidate all queries that contain ['customer', 'list'] in their key
      onSuccess: () =>
        reactQueryClient.invalidateQueries(customerQueryKeys.key("list")),
    }
  );

API

all

ReactQueryKeys.all();

Returns top level key for this instance. You can use this to refer to all other keys created with this instance. For example given a key ['customers', 'list'], reactQueryKeys.all() would return ['customers'].

key

ReactQueryKeys.key(name: string, dynamicValues?: Record<string, any>)

Get the value of a specific key with option to provide an object literal with dynamic values, thus creating different query keys as the given values change.

Configuration

config

| Name | Type | | -------------- | --------------------------------------------------------------------- | | keyDefinitions | Record<string, { childOf?: string, dynamicVariableNames?: string[] }> |

Use the config constructor parameter to create your query keys instance. Each keyName refers to query key you want to be able to use for this instance. You can configure each key to be a child of another key, thus making the key dependent on the childOf key. You can also provide dynamicVariableNames which are the property names for any dynamic values you would like to include in your key. For example to use the value { customerId: '123' } in your key, you would have dynamicVariableNames include 'customerId'.

Credits