All Projects → dano-inc → react-query-helper

dano-inc / react-query-helper

Licence: MIT License
A helper library to use react-query more efficient, consistency

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-query-helper

template-react-native-typescript
Minimal template with best practices and automation scripts for improved developer experience.
Stars: ✭ 19 (-85.71%)
Mutual labels:  react-query
awesome-react-query
A collection of awesome things regarding the React Query ecosystem
Stars: ✭ 67 (-49.62%)
Mutual labels:  react-query
next-app-starter
Another awesome starter for your app base on nextjs + tailwind + react-query + react-hook-form + next-auth + jotai
Stars: ✭ 73 (-45.11%)
Mutual labels:  react-query
start-ui-native
🚀 Start UI [native] is an opinionated UI starter with ⚛️ React Native, ⬢ Native Base 3, ⚛️ React Query & 🐜 Formiz — From the 🐻 BearStudio Team
Stars: ✭ 62 (-53.38%)
Mutual labels:  react-query
next-saas
Rapid development of SaaS products with Next.js
Stars: ✭ 39 (-70.68%)
Mutual labels:  react-query
github-jobs-client
Clone of GitHub Jobs website using the design from Frontend Mentor.
Stars: ✭ 22 (-83.46%)
Mutual labels:  react-query
coderplex-org
Official Website for Coderplex Community. Built with Next.js and deployed on Vercel.
Stars: ✭ 32 (-75.94%)
Mutual labels:  react-query
fullstack-nextjs-ecommerce
Fullstack Next.js E-Commerce made with NextAuth + Prisma, Docker + TypeScript + React Query + Stripe + Tailwind Sentry and much more 🛒
Stars: ✭ 524 (+293.98%)
Mutual labels:  react-query
2021-pick-git
💻 Github Repo 기반 개발 장려 SNS
Stars: ✭ 125 (-6.02%)
Mutual labels:  react-query
react-query-autosync
A react hook which lets you automatically synchronize a value to a server with react-query
Stars: ✭ 35 (-73.68%)
Mutual labels:  react-query
hacker-news-client
Clone of Hacker News with modern design
Stars: ✭ 32 (-75.94%)
Mutual labels:  react-query
react-native-template
An opinionated template to bootstrap your next React Native app with all the time-wasting packages you need to have.
Stars: ✭ 132 (-0.75%)
Mutual labels:  react-query
ndaify-web
NDAify helps you keep your trade secrets under wraps 🔒
Stars: ✭ 33 (-75.19%)
Mutual labels:  react-query
Electrode
Web applications with node.js and React
Stars: ✭ 2,033 (+1428.57%)
Mutual labels:  react-query
expansion-pack
🔋 Useful stack expansion for ts-nextjs-tailwind-starter
Stars: ✭ 16 (-87.97%)
Mutual labels:  react-query
2021-nolto
부담없이 자랑하는 작고 소중한 내 프로젝트 🧸✨
Stars: ✭ 34 (-74.44%)
Mutual labels:  react-query
use-mutation
🧬 Run side-effects safely in React
Stars: ✭ 81 (-39.1%)
Mutual labels:  react-query
tmdb-viewer
A movie browsing/favoriting app
Stars: ✭ 63 (-52.63%)
Mutual labels:  react-query
react-query-blog-demo
An example repo I used to teach a React Query workshop
Stars: ✭ 82 (-38.35%)
Mutual labels:  react-query
react-vite-admin
This Starter utilizes React, Recoil, React Query, React Hooks, Typescript, Axios And Vite. 全新技术栈的后台管理系统
Stars: ✭ 90 (-32.33%)
Mutual labels:  react-query

React Query Helper

npm version npm bundle size npm bundle size Test codecov GitHub

A helper library to help you use React Query more easily and consistently.

Features

  • Write query key once, use everywhere. you don't have to remember something's query key; the query key will be generated with your baseQueryKey and arguments of queryFn that you were called as defined
  • Written in TypeScript. you can infer your query result type/interface.

Why?

I've been using React Query for 2-3 years. In most situations, I used to make a custom hook and manage query keys as constant strings. but it was quite tiring.

Sometimes, It makes me sad when the query data type that I inferred manually is not equal with the actual query data type. for example:

// Set query's data.
queryClient.setQueryData([queryKeys.product.all], [{}] as IProductList);

// It is unknown type:
queryClient.getQueryData([queryKeys.product.all]);

// Okay, we know we can use generic type but we can also make a mistake.
// These mistakes are quite hard to find.
queryClient.getQueryData<IProduct>([queryKeys.product.all]);
queryClient.getQueryData<IProductList>([queryKeys.category.all]);

Essentially, Making a custom hook using useQuery or managing Query Keys as constants strings are not guaranteed your query result type. so you have to infer types manually like queryClient.getQueryData<IProductList> or you'll get unknown type as result.

Installation

NOTE: This libary depends on React Query as a peer dependency.

$ yarn add react-query react-query-helper

# or npm:
$ npm install react-query react-query-helper

Usage

Call makeQueryHelper function to create query helper. Examples:

import { makeQueryHelper } from 'react-query-helper';
import { queryClient } from '../queryClient';
import type { User } from '../types';

export const getUserById = makeQueryHelper({
  queryClient,
  baseQueryKey: ['user'],
  // NOTE: You can access QueryFunctionContext in your function scope.
  queryFn: (context) => (id: number) => {
    return fetch(`https://jsonplaceholder.typicode.com/users/${id}/`).then(
      (response) => response.json() as Promise<User>
    );
  },
});

Now you can use type-safe useQuery and get or set query data type-safety.

import { getUserById } from '../remotes/getUserById';

const UserInfo: FC = () => {
  const { data } = getUserById.useQuery(1);

  return <p>name: {data?.name}</p>;
};

const UserUpdater: FC = () => {
  const handleClick = () => {
    const user = getUserById.getQueryData(1);

    if (user) {
      getUserById.setQueryData(1, {
        ...user,
        name: 'John Doe',
      });
    }
  };

  return <button onClick={handleClick}>Update name</button>;
};

Examples

More examples will be added by library progress.

Contribution

There's no contribute guide. I will write soon. currently, Please consider sharing feedback or reporting an issue.

License

MIT (See the LICENSE file)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].