React Query - woowa-techcamp-2021/store-4 GitHub Wiki

React-query

  • ๋ฐ์ดํ„ฐ fetching, caching, synchronizing and updating server state์„ ๋„์™€์ฃผ๋Š” ๋„๊ตฌ
  • *์ฃผ์˜๋‹˜์ด ๋ฐ์ดํ„ฐ fetching ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ์˜ˆ๋กœ ๋“ค๋ฉด์„œ ๋ง์”€ํ•˜์…จ๋˜ ๊ธฐ๋Šฅ๋“ค ๋Œ€๋ถ€๋ถ„์ด ๊ฐ€๋Šฅํ•œ ๋“ฏ? (์บ์‹ฑ, ์‹คํŒจ์‹œ ์žฌ์š”์ฒญ, ๋ช‡ ์ดˆ๋งˆ๋‹ค ์žฌ์š”์ฒญ ๋“ฑ)
  • ํ•ต์‹ฌ์ด ๋˜๋Š” ๊ฐœ๋…: query, mutation, query invalidation
  • ์•„์ฃผ ๊ฐ„๋‹จํžˆ ์„ค๋ช…ํ•˜๋ฉด query๊ฐ€ ๋ฐ์ดํ„ฐ ๋ฐ›์•„์˜ค๋Š” ๊ฒƒ. mutation์€ post, delete ๊ฐ™์€ ์„œ๋ฒ„ ๋ฆฌ์†Œ์Šค๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ์ž‘์—…. Query invalidation์€ (์ฃผ๋กœ) mutation์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ๋‚œ ๋’ค์— ๊ด€๋ จ query๊ฐ€ ๋” ์ด์ƒ ์œ ํšจํ•˜์ง€ ์•Š๋‹ค(์„œ๋ฒ„๊ฐ€ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐํ•˜๊ณ  ๋‹ค๋ฅด๋‹ค)๊ณ  ์•Œ๋ ค์ฃผ๋Š” ๊ฒƒ. (์˜ˆ๋ฅผ ๋“ค์–ด ๊ธ€์„ ์˜ฌ๋ฆฌ๊ณ  ๋‚˜๋ฉด ์ „์ฒด ๊ธ€ ๋ฆฌ์ŠคํŠธ๋ฅผ invalidate ํ•˜๋Š” ์‹. ๊ทธ๋Ÿฌ๋ฉด ๊ทธ ์ฟผ๋ฆฌ๋Š” ๋‹ค์‹œ fetch๋จ)
  • ๊ธฐ๋ณธ์ (๊ธฐ๋ณธ ์„ธํŒ…์— ์˜ํ•˜๋ฉด)์œผ๋กœ๋Š” ์ฟผ๋ฆฌ์˜ ์ƒˆ ์ธ์Šคํ„ด์Šค๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ด => ์บ์‹œ๋œ ๋ฐ์ดํ„ฐ๋ฅผ staleํ•˜๋‹ค๊ณ  ๋ณด๊ธฐ ๋•Œ๋ฌธ (staleTime ์˜ต์…˜์„ ์ฃผ๋ฉด ๋งค๋ฒˆ ์„œ๋ฒ„ ์š”์ฒญ์„ ํ•˜์ง€ ์•Š๊ณ  ์บ์‹œ์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•จ)
  • query๋Š” ๋ช‡ ๊ฐ€์ง€ ์ƒํƒœ ์กด์žฌ => isLoading, isError, isSuccess, isIdle(?) => ๋‹ค ๋งˆ์น˜๊ณ ๋‚˜๋ฉด inactive (์ด๊ฑด react-query๊ฐ€ ์ž์ฒด์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ์ƒํƒœ์ธ๋“ฏ?)
  • inactiveํ•œ ์ƒํƒœ๋กœ 5๋ถ„์ด ์ง€๋‚œ ์ฟผ๋ฆฌ๋Š” garbage collect
  • ์ด์™ธ์—๋„ background fetching ๊ฐ™์€ ๊ฒƒ๋“ค๋„ ๊ฐ€๋Šฅํ•œ ๋“ฏ
  • ๊ธฐ๋ณธ์ ์ธ ์‚ฌ์šฉ๋ฒ•
  • ๊ฐ ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” useQueryClientํ•ด์„œ ์‚ฌ์šฉ: ์˜ˆ์‹œ (https://react-query.tanstack.com/examples/basic)
  • Custom Hook์œผ๋กœ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ ํŽธํ•จ : ์˜ˆ์‹œ (https://react-query.tanstack.com/examples/custom-hooks)
  • devTools ์žˆ์Œ