[React] React-query์˜ staleTime, cacheTime, invalidateQueries
728x90

Today, What I learned?

์ง€๊ธˆ ํ”„๋กœ์ ํŠธ์—์„œ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋Š” ๋ฆฌ์•กํŠธ ์ฟผ๋ฆฌ์˜ staleTime๊ณผ CacheTime, invalidateQueries์— ๋Œ€ํ•œ ๊ฐœ๋…์ด

์ •๋ฆฝ๋˜์–ด ์žˆ์ง€ ์•Š์€ ๊ฒƒ ๊ฐ™์•„์„œ ํ•œ๋ฒˆ ์ •๋ฆฌํ•˜๋ฉด์„œ ๊ณต์‹๋ฌธ์„œ๋„ ์ฝ์–ด๋ณด๊ณ .. ๋ฏธ๋ž˜์˜ ๋‚˜๋ฅผ ์œ„ํ•œ ๊ธฐ๋ก!..

 

๊ณต์‹ ๋ฌธ์„œ ๋งํฌ๋Š” ์ด์ชฝ์ด๋‹ค.

https://tanstack.com/query/latest/docs/react/overview

 

Overview | TanStack Query Docs

TanStack Query (FKA React Query) is often described as the missing data-fetching library for web applications, but in more technical terms, it makes fetching, caching, synchronizing and updating server state in your web applications a breeze. Motivation

tanstack.com

 

 

staleTime

๋ฆฌ์•กํŠธ ์ฟผ๋ฆฌ๋Š” ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์‘๋‹ต๋ฐ›๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„์ƒํƒœ๋ผ๊ณ  ์ •์˜ํ•œ๋‹ค.
์ด ์„œ๋ฒ„์ƒํƒœ๊ฐ€ ์‹ ์„ ํ•œ(fresh) ๋ฐ์ดํ„ฐ๋กœ ๋™๊ธฐํ™”(refetch)๋  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š”๋ฐ, ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์ผ ๋•Œ ์ž๋™์œผ๋กœ ๋™๊ธฐํ™”๋œ๋‹ค.

  • (๋ฆฌ์•กํŠธ ์ฟผ๋ฆฌ ํ›…์ด ํฌํ•จ๋œ) ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋งˆ์šดํŠธ ๋์„ ๋•Œ
  • ๋„คํŠธ์›Œํฌ๊ฐ€ ๋‹ค์‹œ ์—ฐ๊ฒฐ๋์„ ๋•Œ
  • ๋‹ค๋ฅธ ์ฐฝ์— ์žˆ๋‹ค๊ฐ€ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๋‹ค์‹œ focus ๋์„ ๋•Œ
  • ์„ค์ •ํ•œ refetch interval์— ๋”ฐ๋ผ ๋‹ค์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ

 

staleTime์€ ๋ฐ์ดํ„ฐ์˜ ์‹ ์„ ํ•œ ์ƒํƒœ๊ฐ€ ์œ ์ง€๋˜๋Š” ์‹œ๊ฐ„์„ ๋งํ•œ๋‹ค.
์‹ ์„ ํ•จ์˜ ๊ธฐ์ค€์€ ์„œ๋ฒ„ ์ƒํƒœ๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ํด๋ผ์ด์–ธํŠธ์— ๋„์ฐฉํ•œ ์‹œ์ ๋ถ€ํ„ฐ!..
staleTime์ด ์ ์šฉ๋˜๋Š” ์‹œ๊ฐ„ ๋™์•ˆ์€ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ค์‹œ ํ˜ธ์ถœ๋˜์ง€ ์•Š๊ณ , ์ด๋ฏธ ์บ์‹ฑ๋œ ์„œ๋ฒ„ ์ƒํƒœ๋ฅผ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.

 

 

๐Ÿค” ๊ทธ๋Ÿผ staleTime์ด ์ง€๋‚˜๋ฉด??

๊ทธ๋•Œ๋ถ€ํ„ฐ๋Š” stale ์ƒํƒœ๊ฐ€ ๋˜์–ด refetch๊ฐ€ ํ•„์š”ํ•œ ์ƒํ™ฉ์— refetch ๋˜์–ด ์ตœ์‹  ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

staleTime์€ ๊ธฐ๋ณธ์ ์œผ๋กœ 0์œผ๋กœ ์„ค์ •๋˜์–ด ์žˆ์–ด์„œ, staleTime์„ ์„ค์ •ํ•ด ์ฃผ๋ฉด

๋ฆฌ์•กํŠธ ์ฟผ๋ฆฌ์—๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ตœ์‹ ํ™”ํ•  ๊ธฐ์ค€์„ ์‹ฌ์–ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ๋ถˆํ•„์š”ํ•œ refetch๋ฅผ ๋ง‰์„ ์ˆ˜ ์žˆ๋‹ค.

 

 

react-query-devtool๋กœ ๋ฐ์ดํ„ฐ์˜ ์ƒํƒœ ํŒŒ์•…ํ•˜๊ธฐ

๋ฆฌ์•กํŠธ ์ฟผ๋ฆฌ์—์„œ ์ œ๊ณตํ•˜๋Š” devtool์„ ์ด์šฉํ•˜๋ฉด ๋ฐ์ดํ„ฐ๊ฐ€ fresh ํ•œ ์ง€ stale ํ•œ ์ง€ ํ•œ๋ˆˆ์— ํ™•์ธํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค! ๐Ÿ‘

์•„๋ž˜ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” 3๊ฐœ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ null๊ฐ’์ด ์„ค์ •๋˜์–ด ์žˆ๊ณ  disbled์ฒ˜๋ฆฌ๊ฐ€ ๋˜์–ด์žˆ๋‹ค. (๋กœ๊ทธ์ธ ํ›„์— ํ™œ์„ฑํ™”๋˜๋Š” ๋ฐ์ดํ„ฐ๋“ค)

๋กœ๊ทธ์ธ์„ ํ•˜๋ฉด stale ํ–ˆ๋˜ ๋ฐ์ดํ„ฐ๋“ค์ด fresh ํ•ด์ง„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋กœ๊ทธ์ธ ํ›„ ๊ด€๋ จ ์ปดํฌ๋„ŒํŠธ๋“ค์ด ๋‹ค์‹œ ๋งˆ์šดํŠธ ๋˜๋ฉด์„œ disabled ๋˜์–ด์žˆ๋˜ ๊ฒƒ์ด ์‚ฌ๋ผ์กŒ๊ธฐ ๋•Œ๋ฌธ..

 

 

 

cacheTime

์ฟผ๋ฆฌ๊ฐ€ inactive ์ƒํƒœ๊ฐ€ ๋˜๋ฉด cacheTIme๋™์•ˆ ๋ฉ”๋ชจ๋ฆฌ์— ์กด์žฌํ•˜๋‹ค๊ฐ€ ๊ฐ€๋น„์ง€์ปฌ๋ ‰ํ„ฐ๋กœ ์ˆ˜์ง‘๋˜์–ด ์‚ฌ๋ผ์ง„๋‹ค.

 

๐Ÿค” inactive ์ƒํƒœ??
์ฟผ๋ฆฌ ํ›…์„ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์–ธ๋งˆ์šดํŠธ ๋œ ์ƒํƒœ!

 

 

๐Ÿค” ๊ทธ๋Ÿผ cacheTIme ์•ˆ์— ๋‹ค์‹œ ์ฟผ๋ฆฌ๊ฐ€ ๋งˆ์šดํŠธ ๋œ๋‹ค๋ฉด??

์บ์‹œ ์ €์žฅ์†Œ์— ์žˆ์—ˆ๋˜ ๊ฐ’์ด ์šฐ์„ ์ ์œผ๋กœ ๋ฐ˜ํ™˜๋˜์ง€๋งŒ staleTime์„ ๊ฒฝ๊ณผํ•œ ๊ฒฝ์šฐ์—๋Š” refetch ๋  ์ˆ˜ ์žˆ๋‹ค!

staleTime์€ ์„œ๋ฒ„๊ฐ€ ํด๋ผ์ด์–ธํŠธ์— ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ ์‹œ์ ๋ถ€ํ„ฐ ์‹œ์ž‘๋˜์ง€๋งŒ cacheTime์€ inactive ์ƒํƒœ๋ถ€ํ„ฐ ํƒ€์ด๋จธ๋ฅผ ์‹œ์ž‘ํ•œ๋‹ค๋Š” ์ !..

 

 

invalidateQueries()

invalidateQueries๋Š” queryClient์˜ ๋ฉ”์„œ๋“œ ์ค‘ ํ•˜๋‚˜๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ๋จผ์ € ์„ ์–ธ์ด ๋˜์–ด์žˆ์–ด์•ผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ!

  const queryClient = useQueryClient();

๊ณต์‹ ๋ฌธ์„œ์— ๋”ฐ๋ฅด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์บ์‹œ ์•ˆ์˜ ๋ชจ๋“  ์ฟผ๋ฆฌ๋ฅผ ๋ฌดํšจํ™”ํ•œ๋‹ค.
๋งŒ์•ฝ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฟผ๋ฆฌํ‚ค๊ฐ€ ์ง€์ •๋˜์–ด ์žˆ๋‹ค๋ฉด, ํ•ด๋‹น ์ฟผ๋ฆฌํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ์ฟผ๋ฆฌ๋งŒ ๋ชจ๋‘ ๋ฌดํšจํ™”๋œ๋‹ค.

// Invalidate every query in the cache
queryClient.invalidateQueries()
// Invalidate every query with a key that starts with `todos`
queryClient.invalidateQueries({ queryKey: ['todos'] })

 

 

 

๐Ÿค” ์˜ˆ์‹œ๋ฅผ ๋“ค์ž๋ฉด?

 

์šฐ๋ฆฌ ํ”„๋กœ์ ํŠธ์— ์žˆ๋Š” ๊ด€์‹ฌ(์ข‹์•„์š”) ๊ธฐ๋Šฅ.
์•„๋ž˜ ๋ฎคํ…Œ์ด์…˜์ด ์‹คํ–‰๋˜๊ณ  ์„ฑ๊ณตํ–ˆ์„ ๋•Œ, ์ข‹์•„์š” ๊ด€๋ จ ์ฟผ๋ฆฌ๋“ค์ด ๊ฐฑ์‹ ๋˜์–ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฟŒ๋ ค์ค˜์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— invalidate ๋˜๊ณ  ์žˆ๋‹ค.

  const { mutate: updateMyProjectMutate } = useMutation(
    () => updateMyProject(uid, pid, isLike),
    {
      onSuccess: () => {
        queryClient.invalidateQueries(['myProjects', uid]);
        queryClient.invalidateQueries(['post', 'mostViewed']);
        queryClient.invalidateQueries(['post', 'mostLiked']);
        queryClient.invalidateQueries(['likedProjects', uid]);
      },
    },
  );

 

 

์ข‹์•„์š”๊ฐ€ ์ถ”๊ฐ€๋˜๊ฑฐ๋‚˜ ์‚ฌ๋ผ์งˆ ๋•Œ๋งˆ๋‹ค ์ฟผ๋ฆฌ๊ฐ€ ๋ฌดํšจํ™”๋˜๊ธฐ ๋•Œ๋ฌธ์—

ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ค๋Š” ๋ถ€๋ถ„์—์„œ๋Š” useQuery๊ฐ€ ์ƒˆ๋กญ๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ , ์ตœ์‹ ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค!

  // ์œ ์ € ํ”„๋กœ์ ํŠธ ๋ฆฌ์ŠคํŠธ ๋ฐ›์•„์˜ค๋Š” ์ฟผ๋ฆฌ
  const { data: userProjectListsData }: any = useQuery({
    queryKey: ['myProjects', uid],
    queryFn: getUserProjectList,
    staleTime: staleTime.myProjects,
    enabled: !!uid,
  });

 

 

 

 

 

728x90