Skip to content

Latest commit

ย 

History

History
384 lines (286 loc) ยท 11.9 KB

queryClient.md

File metadata and controls

384 lines (286 loc) ยท 11.9 KB

๐Ÿ’ป QueryClient ์ฃผ์š” ๋‚ด์šฉ

QueryClient

  • react-query์—์„œ QueryClient์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์บ์‹œ์™€ ์ƒํ˜ธ์ž‘์šฉ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ธฐ๋ณธ์ ์ธ ์‚ฌ์šฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
// v4
import { QueryClient } from "@tanstack/react-query";

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      staleTime: Infinity,
    },
  },
});

// v3
import { QueryClient } from "react-query";

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      staleTime: Infinity,
    },
  },
});

Option

  • queryCache?: QueryCache
    • Optional
    • ํ•ด๋‹น queryClient ์—ฐ๊ฒฐ๋œ query ์บ์‹œ ์ž…๋‹ˆ๋‹ค.
    • ์ž˜ ์•ˆ์“ฐ์ด์ง€๋Š” ์•Š์Œ
  • mutationCache?: MutationCache
    • Optional
    • ํ•ด๋‹น queryClient์™€ ์—ฐ๊ฒฐ๋œ mutation cache์ž…๋‹ˆ๋‹ค.
    • ์ž˜ ์•ˆ์“ฐ์ด์ง€๋Š” ์•Š์Œ
  • logger?: Logger
    • v4
    • Optional
    • ํ•ด๋‹น queryClient์˜ ๋””๋ฒ„๊น… ์ •๋ณด, ๊ฒฝ๊ณ  ๋ฐ ์˜ค๋ฅ˜๋ฅผ ๊ธฐ๋กํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ๋กœ๊ฑฐ์ž…๋‹ˆ๋‹ค. ์„ค์ •ํ•˜์ง€ ์•Š์œผ๋ฉด console๊ธฐ๋ณธ ๋กœ๊ฑฐ์ž…๋‹ˆ๋‹ค.
  • defaultOptions?: DefaultOptions
    • Optional
    • ๋ชจ๋“  query ๋ฐ mutation์— ๋Œ€ํ•œ ๊ธฐ๋ณธ๊ฐ’์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
    • ์ž์ฃผ ์“ฐ์ž„ โญ๏ธ

์ž์ฃผ ์“ฐ์ด๋Š” QueryClient ์˜ต์…˜

๐Ÿ“ƒ ๋ชฉ์ฐจ

  1. queryClient.useQueryClient
  2. queryClient.getQueryData
  3. queryClient.getQueriesData
  4. queryClient.setQueryData
  5. queryClient.setQueriesData
  6. queryClient.invalidateQueries
  7. queryClient.refetchQueries
  8. queryClient.cancelQueries
  9. queryClient.removeQueries
  10. queryClient.resetQueries
  11. queryClient.clear

๐Ÿ™ ์ฐธ๊ณ ๋กœ v3 ํ˜•ํƒœ๋ฅผ v4์—์„œ ๋Œ€๋ถ€๋ถ„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค ๐Ÿ™


useQueryClient

  • ์ผ๋ฐ˜์ ์œผ๋กœ QueryClient์˜ ์˜ต์…˜๋“ค์„ ์ด์šฉํ•  ๋•Œ๋Š” ํ˜„์žฌ QueryClient์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” useQueryClient Hook์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
// v3
import { useQueryClient } from "react-query";

const queryClient = useQueryClient();

// v4
import { useQueryClient } from "@tanstack/react-query";

const queryClient = useQueryClient({ context });
// context?: React.Context<QueryClient | undefined>
// ์‚ฌ์šฉ์ž ์ •์˜ React Query Context๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ํ•ด๋‹น ์˜ต์…˜ ์‚ฌ์šฉํ•จ

getQueryData

  • queryClient.getQueryData๋Š” ๊ธฐ์กด ์ฟผ๋ฆฌ์˜ ์บ์‹œ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋™๊ธฐ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฟผ๋ฆฌ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด undefined๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
// v3
const data = queryClient.getQueryData(queryKey);

// v4
const data = queryClient.getQueryData({ queryKey });
// ์‹ค์ œ ์˜ˆ์ œ
const queryData = queryClient.getQueryData(["super-heroes"]);
// type
getQueryData<TQueryFnData = unknown>(queryKey: QueryKey, filters?: QueryFilters): TQueryFnData | undefined;

getQueriesData

  • queryClient.getQueriesData๋Š” ์—ฌ๋Ÿฌ ์ฟผ๋ฆฌ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋™๊ธฐ ํ•จ์ˆ˜ ์ž…๋‹ˆ๋‹ค. ์ „๋‹ฌ๋œ queryKey ๋˜๋Š” filters์™€ ์ผ์น˜ํ•˜๋Š” ์ฟผ๋ฆฌ๋งŒ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ผ์น˜ํ•˜๋Š” ์ฟผ๋ฆฌ๊ฐ€ ์—†์œผ๋ฉด ๋นˆ ๋ฐฐ์—ด์ด ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.
  • Query filters
// v3/v4
const data = queryClient.getQueriesData(queryKey | filters);
  • v4์—์„œ๋Š” v3 ํ˜•ํƒœ๋„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค
// type
getQueriesData<TQueryFnData = unknown>(queryKey: QueryKey): [QueryKey, TQueryFnData | undefined][];
getQueriesData<TQueryFnData = unknown>(filters: QueryFilters): [QueryKey, TQueryFnData | undefined][];

setQueryData

  • queryClient.setQueryData๋Š” ์บ์‹ฑ๋œ ์ฟผ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ฆ‰์‹œ ์—…๋ฐ์ดํŠธํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋™๊ธฐ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • invalidateQueries์™€ ๋”๋ถˆ์–ด ์บ์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ์ตœ์‹ ํ™”ํ•  ๋•Œ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค.
// v3/v4
queryClient.setQueryData(["super-hero"], (oldData) => {
  return {
    ...oldData,
    data: [...oldData.data, data.data],
  };
});
  • v4์—์„œ๋Š” v3 ํ˜•ํƒœ๋„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค
// type
setQueryData<TQueryFnData>(queryKey: QueryKey, updater: Updater<TQueryFnData | undefined, TQueryFnData | undefined>, options?: SetDataOptions): TQueryFnData | undefined;

setQueriesData

  • queryClient.setQueriesData๋Š” ํ•„ํ„ฐ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์ฟผ๋ฆฌ ํ‚ค๋ฅผ ๋ถ€๋ถ„์ ์œผ๋กœ ์ผ์น˜์‹œ์ผœ ์—ฌ๋Ÿฌ ์ฟผ๋ฆฌ์˜ ์บ์‹œ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฆ‰์‹œ ์—…๋ฐ์ดํŠธํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋™๊ธฐ ํ•จ์ˆ˜์ด๋‹ค.
  • ์ „๋‹ฌ๋œ queryKey ๋˜๋Š” filters์™€ ์ผ์น˜ํ•˜๋Š” ์ฟผ๋ฆฌ๋งŒ ์—…๋ฐ์ดํŠธ๋˜๋ฉฐ, ์ƒˆ ์บ์‹œ ํ•ญ๋ชฉ์ด ์ƒ์„ฑ๋˜์ง€ ์•Š๋Š”๋‹ค.
// v3/v4
queryClient.setQueriesData(["super-hero"], (oldData) => {
  return {
    ...oldData,
    data: [...oldData.data, data.data],
  };
});
  • v4์—์„œ๋Š” v3 ํ˜•ํƒœ๋„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค
setQueriesData<TQueryFnData>(queryKey: QueryKey, updater: Updater<TQueryFnData | undefined, TQueryFnData | undefined>, options?: SetDataOptions): [QueryKey, TQueryFnData | undefined][];
setQueriesData<TQueryFnData>(filters: QueryFilters, updater: Updater<TQueryFnData | undefined, TQueryFnData | undefined>, options?: SetDataOptions): [QueryKey, TQueryFnData | undefined][];

invalidateQueries

  • queryClient.invalidateQueries๋Š” setQueryData์™€ ๋”๋ถˆ์–ด ์บ์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ์ตœ์‹ ํ™”ํ•  ๋•Œ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. (์ฟผ๋ฆฌ ๋ฌดํšจํ™” ์˜ˆ์ œ) ๋‹จ์ผ ๋˜๋Š” ์—ฌ๋Ÿฌ ์ฟผ๋ฆฌ๋ฅผ ๋ฌดํšจํ™”ํ•˜๊ณ , ๋‹ค์‹œ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค.
  • ๊ธฐ๋ณธ์ ์œผ๋กœ ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ์ฟผ๋ฆฌ๋Š” ์ฆ‰์‹œ ์œ ํšจํ•˜์ง€ ์•Š์€ ๊ฒƒ์œผ๋กœ ํ‘œ์‹œ๋˜๊ณ , ํ™œ์„ฑ ์ฟผ๋ฆฌ๋Š” ๋ฐฑ๋“œ๋ผ์šด๋“œ์—์„œ ๋‹ค์‹œ ๊ฐ€์ ธ์˜จ๋‹ค.
  • ๋งŒ์•ฝ, ํ™œ์„ฑ ์ฟผ๋ฆฌ๋ฅผ ๋‹ค์‹œ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์„ ์›ํ•˜์ง€ ์•Š์œผ๋ฉด v3์—์„œ๋Š” refetchActive: false, v4์—์„œ๋Š” refetchType: 'none'๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋ฐ˜๋Œ€๋กœ ๋น„ ํ™œ์„ฑํ™” ์ฟผ๋ฆฌ๋ฅผ ๋‹ค์‹œ ๊ฐ€์ ธ์˜ค๊ธฐ๋ฅผ ์›ํ•œ๋‹ค๋ฉด v3์—์„œ๋Š” refetchInactive: true, v4์—์„œ๋Š” refetchType: 'all'์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ฐธ๊ณ ๋กœ query ์˜ต์…˜์œผ๋กœ enabled: false ์˜ต์…˜์„ ์ฃผ๋ฉด queryClient๊ฐ€ ์ฟผ๋ฆฌ๋ฅผ ๋‹ค์‹œ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•๋“ค ์ค‘ invalidateQueries์™€ refetchQueries๋ฅผ ๋ฌด์‹œํ•œ๋‹ค.
// v3
await queryClient.invalidateQueries(
  ["super-heroes"],
  {
    exact,
    refetchActive: true,
    refetchInactive: false,
  },
  { throwOnError, cancelRefetch }
);

// v4
await queryClient.invalidateQueries(
  {
    queryKey: ["super-heroes"],
    exact,
    refetchType: "active",
  },
  { throwOnError, cancelRefetch }
);

// exact์˜ต์…˜์„ ์คฌ๊ธฐ ๋•Œ๋ฌธ์— ์ฟผ๋ฆฌ ํ‚ค์™€ ์ •ํ™•ํžˆ ์ผ์น˜ํ•˜๋Š” ์ฟผ๋ฆฌ๋งŒ์„ ๋ฌดํšจํ™”ํ•˜๊ณ  ๋‹ค์‹œ ๊ฐ€์ ธ์˜จ๋‹ค.
  • ์ถ”๊ฐ€์ ์œผ๋กœ invalidateQueries๊ฐ€ ๋ฌดํšจํ™”ํ•˜๋Š” ์ฟผ๋ฆฌ ๋ฒ”์œ„๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ์ƒ์œ„ -> ํ•˜์œ„๋กœ ์ „ํŒŒ๋œ๋‹ค. ์ด๊ฒŒ ๋ฌด์Šจ ๋ง์ด๋ƒ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ['super-heros'] ์ฟผ๋ฆฌ๋ฅผ ๋ฌดํšจํ™” ํ•˜๊ฒŒ ๋˜๋ฉด ์•„๋ž˜ ํ•˜์œ„ ์ฟผ๋ฆฌ๋“ค๋„ ๋ชจ๋‘ ์ดˆ๊ธฐํ™”๋œ๋‹ค.
queryClient.invalidateQueries({
  queryKey: ["super-heroes"],
});

["super-heros"],
["super-heros", 'superman'],
["super-heros", { id: 1} ],
  • ์œ„์™€ ๊ฐ™์ด ['super-heros'] ์ฟผ๋ฆฌ๋ฅผ ๋ฌดํšจํ™” ํ•˜๊ฒŒ ๋˜๋ฉด ์•„๋ž˜ ํ•˜์œ„ ์ฟผ๋ฆฌ๋“ค๋„ ๋ชจ๋‘ ์ดˆ๊ธฐํ™”๋œ๋‹ค.
  • ํ•˜์ง€๋งŒ ํ•ด๋‹น key๋งŒ ๋ฌดํšจํ™” ์‹œํ‚ค๋ ค๋ฉด ์ฒซ ๋ฒˆ์งธ ์˜ˆ์ œ ์ฝ”๋“œ์—์„œ๋„ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด exact ์˜ต์…˜์„ ์ฃผ๋ฉด ๋œ๋‹ค.

  • v4์—์„œ๋Š” v3 ํ˜•ํƒœ๋„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค
// type
invalidateQueries<TPageData = unknown>(filters?: InvalidateQueryFilters<TPageData>, options?: InvalidateOptions): Promise<void>;
invalidateQueries<TPageData = unknown>(queryKey?: QueryKey, filters?: InvalidateQueryFilters<TPageData>, options?: InvalidateOptions): Promise<void>;

refetchQueries

  • queryClient.refetchQueries๋Š” ํŠน์ • ์กฐ๊ฑด์— ๋”ฐ๋ผ ์ฟผ๋ฆฌ๋ฅผ ๋‹ค์‹œ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.
  • ์ฐธ๊ณ ๋กœ query ์˜ต์…˜์œผ๋กœ enabled: false ์˜ต์…˜์„ ์ฃผ๋ฉด queryClient๊ฐ€ ์ฟผ๋ฆฌ๋ฅผ ๋‹ค์‹œ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•๋“ค ์ค‘ invalidateQueries์™€ refetchQueries๋ฅผ ๋ฌด์‹œํ•œ๋‹ค.
// v3
// ๋ชจ๋“  ์ฟผ๋ฆฌ๋ฅผ ๋‹ค์‹œ ๊ฐ€์ ธ์˜จ๋‹ค
await queryClient.refetchQueries();

// ๋ชจ๋“  stale ์ƒํƒœ์˜ ์ฟผ๋ฆฌ๋ฅผ ๋‹ค์‹œ ๊ฐ€์ ธ์˜จ๋‹ค.
await queryClient.refetchQueries({ stale: true });

// ์ฟผ๋ฆฌ ํ‚ค์™€ ๋ถ€๋ถ„์ ์œผ๋กœ ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ํ™œ์„ฑ ์ฟผ๋ฆฌ๋ฅผ ๋‹ค์‹œ ๊ฐ€์ ธ์˜จ๋‹ค.
await queryClient.refetchQueries(["super-heroes"], { active: true });

// exact ์˜ต์…˜์„ ์คฌ๊ธฐ ๋•Œ๋ฌธ์— ์ฟผ๋ฆฌ ํ‚ค์™€ ์ •ํ™•ํžˆ ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ํ™œ์„ฑ ์ฟผ๋ฆฌ๋ฅผ ๋‹ค์‹œ ๊ฐ€์ ธ์˜จ๋‹ค.
await queryClient.refetchQueries(["super-heroes", 1], {
  active: true,
  exact: true,
});
// v4
// ๋ชจ๋“  ์ฟผ๋ฆฌ๋ฅผ ๋‹ค์‹œ ๊ฐ€์ ธ์˜จ๋‹ค
await queryClient.refetchQueries();

// ๋ชจ๋“  stale ์ƒํƒœ์˜ ์ฟผ๋ฆฌ๋ฅผ ๋‹ค์‹œ ๊ฐ€์ ธ์˜จ๋‹ค.
await queryClient.refetchQueries({ stale: true });

// ์ฟผ๋ฆฌ ํ‚ค์™€ ๋ถ€๋ถ„์ ์œผ๋กœ ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ํ™œ์„ฑ ์ฟผ๋ฆฌ๋ฅผ ๋‹ค์‹œ ๊ฐ€์ ธ์˜จ๋‹ค.
await queryClient.refetchQueries({
  queryKey: ["super-heroes"],
  type: "active",
});

// exact ์˜ต์…˜์„ ์คฌ๊ธฐ ๋•Œ๋ฌธ์— ์ฟผ๋ฆฌ ํ‚ค์™€ ์ •ํ™•ํžˆ ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ํ™œ์„ฑ ์ฟผ๋ฆฌ๋ฅผ ๋‹ค์‹œ ๊ฐ€์ ธ์˜จ๋‹ค.
await queryClient.refetchQueries({
  queryKey: ["super-heroes", 1],
  type: "active",
  exact: true,
});

await queryClient.refetchQueries(
  {
    queryKey: ["super-heroes", 1],
    type: "active",
    exact: true,
  },
  { throwOnError, cancelRefetch }
);
  • v4์—์„œ๋Š” v3ํ˜•ํƒœ๋กœ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.
// type
refetchQueries<TPageData = unknown>(filters?: RefetchQueryFilters<TPageData>, options?: RefetchOptions): Promise<void>;
refetchQueries<TPageData = unknown>(queryKey?: QueryKey, filters?: RefetchQueryFilters<TPageData>, options?: RefetchOptions): Promise<void>;

cancelQueries

  • queryClient.cancelQueries๋Š” ๋‚˜๊ฐ€๊ณ  ์žˆ๋Š” ์•ก์„ธ์Šค ๊ฐ€๋Šฅํ•œ ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜๋™์ ์œผ๋กœ ์ทจ์†Œ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜์ด๋‹ค.
  • ํ•ด๋‹น ํ•จ์ˆ˜๋Š” ๋‚˜๊ฐ€๊ณ  ์žˆ๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜๋™์œผ๋กœ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‚™๊ด€์  ์—…๋ฐ์ดํŠธ๋ฅผ ํ•  ๋•Œ ์ˆ˜ํ–‰ํ•  ๋•Œ ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค. (๋‚™๊ด€์  ์—…๋ฐ์ดํŠธ ์ฐธ๊ณ )
// v3
await queryClient.cancelQueries(["super-heroes"], { exact: true });

// v4
await queryClient.cancelQueries({ queryKey: ["super-heroes"], exact: true });
  • v4์—์„œ๋Š” v3ํ˜•ํƒœ๋„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.
// type
cancelQueries(filters?: QueryFilters, options?: CancelOptions): Promise<void>;
cancelQueries(queryKey?: QueryKey, filters?: QueryFilters, options?: CancelOptions): Promise<void>;

removeQueries

  • queryClient.removeQueries๋Š” ์•ก์„ธ์Šค ๊ฐ€๋Šฅํ•œ ์บ์‹œ ์ฟผ๋ฆฌ๋ฅผ ์ œ๊ฑฐํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.
// v3
queryClient.removeQueries(["super-heroes"], { exact: true });

// v4
queryClient.removeQueries({ queryKey: ["super-heroes"], exact: true });
  • v4์—์„œ๋Š” v3ํ˜•ํƒœ๋„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.
removeQueries(filters?: QueryFilters): void;
removeQueries(queryKey?: QueryKey, filters?: QueryFilters): void;

resetQueries

  • queryClient.resetQueries๋Š” ์•ก์„ธ์Šค ๊ฐ€๋Šฅํ•œ ์บ์‹œ ์ฟผ๋ฆฌ๋ฅผ ์ดˆ๊ธฐ ์ƒํƒœ๋กœ ์žฌ์„ค์ •ํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.
  • ํ•ด๋‹น ํ•จ์ˆ˜๋Š” clear์™€๋Š” ๋‹ฌ๋ฆฌ ๋ชจ๋“  ๊ตฌ๋…์ž๋ฅผ ์ œ๊ฑฐํ•˜์ง€ ์•Š๊ณ  ๊ตฌ๋…์ž๋“ค์—๊ฒŒ ์•Œ๋ฆฐ๋‹ค. ๋˜ํ•œ invalidateQueries์™€๋Š” ๋‹ฌ๋ฆฌ ์ฟผ๋ฆฌ๋ฅผ ๋ฏธ๋ฆฌ ๋กœ๋“œ๋œ ์ƒํƒœ๋กœ ์žฌ์„ค์ •ํ•œ๋‹ค.
  • ๋งŒ์•ฝ, ์ฟผ๋ฆฌ ์˜ต์…˜์œผ๋กœ initialData๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ์ฟผ๋ฆฌ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋กœ ์žฌ์„ค์ •๋œ๋‹ค.
// v3
queryClient.resetQueries(["super-heroes"], { exact: true });

// v4
queryClient.resetQueries({ queryKey: ["super-heroes"], exact: true });
  • v4์—์„œ๋Š” v3ํ˜•ํƒœ๋„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.
// type
resetQueries<TPageData = unknown>(filters?: ResetQueryFilters<TPageData>, options?: ResetOptions): Promise<void>;
resetQueries<TPageData = unknown>(queryKey?: QueryKey, filters?: ResetQueryFilters<TPageData>, options?: ResetOptions): Promise<void>;

clear

  • queryClient.clear๋Š” ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ์บ์‹œ๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.
queryClient.clear();