simple-react-query

Use react query lightly.

Usage no npm install needed!

<script type="module">
  import simpleReactQuery from 'https://cdn.skypack.dev/simple-react-query';
</script>

README

Simple React Query โญ๏ธ


A Light Weight Data Fetching Library

  • ๐Ÿ“ฆ Unpacked Size: only 17.7KB (react-query is 2.18MB)

Features

  • โœ… Simple useQuery
  • โœ… Simple useMutation
  • โœ… Simple refetchInterval & clearRefetchInterval
  • โœ… Setting initialData
  • โœ… Caching is not a default (It's optional)

Backgrounds

1. react-query is heavy

  • ๋ฆฌ์•กํŠธ ์ฟผ๋ฆฌ๋Š” ๋ฌด๊ฒ์Šต๋‹ˆ๋‹ค

2. Too many methods, but I need only some

  • ๋ฆฌ์•กํŠธ ์ฟผ๋ฆฌ๋Š” ๋„ˆ๋ฌด ๋งŽ์€ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
  • ๋‚˜๋Š” ๋‹จ์ง€ ๋น„๋™๊ธฐ ์ƒํƒœ๊ด€๋ฆฌ๋ฅผ ํ•˜๊ณ ์‹ถ์„ ๋ฟ์ธ๋ฐ, ๋น„๋™๊ธฐ์— ๋Œ€ํ•œ ๋ชจ๋“  ๋„๊ตฌ์™€ ์„ค๋ช…์„ ์ œ๊ณตํ•ด์ค๋‹ˆ๋‹ค.
  • ๋Œ€๋ถ€๋ถ„์˜ ๊ธฐ๋Šฅ์€ ํ•„์š”์—†๊ฑฐ๋‚˜, ์Šค์Šค๋กœ ์ถฉ๋ถ„ํžˆ ๊ตฌํ˜„๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

3. When bugs occurred, debugging is hell

  • ๋ฆฌ์•กํŠธ ์ฟผ๋ฆฌ์˜ ๋‚ด๋ถ€ ๋™์ž‘์„ ์•Œ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
  • ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋ณต์žกํ• ์ˆ˜๋ก, ๋ฒ„๊ทธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„๋•Œ ๋””๋ฒ„๊น…ํ•˜๊ธฐ ๋ณต์žกํ•ฉ๋‹ˆ๋‹ค.

4. Interface of useMutation is different from that of useQuery

  • useMutation์€ .mutateAsync ํ˜ธ์ถœ์‹œ ์—๋Ÿฌ๋ฅผ throwํ–ˆ์„๋•Œ try/catch๋กœ ์žก์•„์ค˜์•ผํ•˜๋Š” ๋ฐ˜๋ฉด, useQuery๋Š” ์—๋Ÿฌ๋ฅผ throwํ–ˆ์„๋–„ error ๊ฐ์ฒด์— ์ž๋™์œผ๋กœ ๋ฐ˜์˜๋˜์–ด์„œ ์‚ฌ์šฉ์˜ ์ผ๊ด€์„ฑ์ด ์—†์Šต๋‹ˆ๋‹ค.

5. useQuery data type must include "undefined"

  • react-query์˜ useQuery dataํƒ€์ž…์€ ํ•ญ์ƒ undefined ํƒ€์ž…์„ union์œผ๋กœ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
  • ์˜ˆ๋ฅผ๋“ค์–ด, useQuery์˜ ํƒ€์ž…์„ string์œผ๋กœ ๋‘๋ฉด string | undefined์˜ dataํƒ€์ž…์„ ๊ฐ€์ง€๊ณ  initialData๋ฅผ null ๋กœ ์„ค์ •ํ•˜๋ฉด data ํƒ€์ž…์ด string | null | undefined ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

6. Difficult to cancel refreshInterval setting

  • react-query์˜ refreshInterval์€ ์ฃผ๊ธฐ์ ์ธ ์š”์ฒญ์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ์ง€๋งŒ, ํ•ด๋‹น ์ฃผ๊ธฐ์ธ ์š”์ฒญ์„ ์ทจ์†Œํ•˜๋ ค๋ฉด axios์˜ cancel ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๋“ฑ์˜ ๋ณต์žกํ•œ ๊ตฌํ˜„์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

7. react-query caching is a default

  • ์บ์‹ฑ์„ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ•ด์„œ, ๊ฐ€๋”์”ฉ ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

Usage

Install

yarn add simple-react-query

QueryClient

QueryClient has queryCache.

import { QueryClient, QueryClientProvider } from "simple-react-query";

<QueryClientProvider value={QueryClient}>
  <App />
</QueryClientProvider>;

useQuery

const {
  refetch,
  isLoading,
  isError,
  data,
  error,
  setData,
  isSuccess,
  clearRefetchInterval,
  isFetched
} = useQuery<TypeOfResponseData>({
  enabled: true,
  query: () => fetch(...),
  initialData: {},
  onSuccess: () => console.log("fetch success!"),
  refetchInterval: 5000,
  isEqualToPrevDataFunc: (a,b) => a.id === b.id,
  cacheTime: 5000,
  queryKeys: ["User", "id"]
});

Props

  1. enabled: auto fetch, when useQuery called
  2. query: fetch function
  3. onSuccess (optional): action after query fetched successfully
  4. initialData (optional): set initial data
  5. refetchInterval (optional): refetch interval (ms)(background ok)
  6. isEqualToPrevDataFunc (optional): when newData fetched, isEqualToPrevDataFunc called with (newData, prevData), if false update newData, true don't update newData because it is same.
  7. cacheTime (optional): caching time useQuery's return data. (ms) default is 0.
  8. queryKeys (optional): caching is decided by queryKeys. It must be array.

Returns

  1. refetch: refetch query
  2. isLoading: fetch is not complete
  3. isError: fetch has error
  4. data: fetch's return data (or cached data)
  5. error: error object
  6. setData: update data state
  7. isSuccess: fetch is complete successfully
  8. clearRefetchInterval: clear interval refetch
  9. isFetched: query is fetched more then 1 time

useMutation

const { isLoading, isError, error, data, mutation } = useMutation<
  TypeRequestData,
  TypeResponseData
>({
  query: (_data: TypeRequestData) => fetch(_data),
  onSuccess: () => {
    console.log("mutation successfully");
  }
});

Props

  1. query: mutation fetch function
  2. onSuccess (optional): action after query fetched successfully

Returns

  1. isLoading: fetch is not complete
  2. isError: fetch has error
  3. data: fetch's return data
  4. error: error object
  5. mutation: wrapped async fetch function, use this function instead fetch

Has Better Idea?

  • When you want another method, contact definedable@gmail.com or custom yourself this code. It is very simple!