Skip to content

Latest commit

ย 

History

History
302 lines (203 loc) ยท 9.86 KB

week4.md

File metadata and controls

302 lines (203 loc) ยท 9.86 KB

๐Ÿต ๋กœ๋”ฉ, ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š” ๋ฐฉ๋ฒ•

  • ๋กœ๋”ฉ๊ณผ ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋Š” ์ด์œ ๋Š” ๋ญ˜๊นŒ?

    • ๋ฐ์ดํ„ฐ ํŽ˜์นญ์— ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ ๊ฒฝ์šฐ, ์—๋Ÿฌ ๋ฐœ์ƒ ์—†์ด ๋ฐ์ดํ„ฐ ํŽ˜์นญ์ด ์ž˜๋ชป๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์„ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋ฉˆ์ถ”์ง€ ์•Š๊ณ  ์•Œ๋ ค์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
    • ๋˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ค‘์ธ ๋กœ๋”ฉ ์ƒํƒœ๋ฅผ ์ฒ˜๋ฆฌํ•˜์—ฌ ๋กœ๋”ฉ ํŽ˜์ด์ง€๋ฅผ ๋„์šฐ๋Š” ๋“ฑ ์‚ฌ์šฉ์ž ๊ฒฝํ—˜์„ ํ–ฅ์ƒ์‹œํ‚ค๋Š” ๋ฐ์— ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • React Query๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋กœ๋”ฉ, ์—๋Ÿฌ์ฒ˜๋ฆฌ ํ•˜๊ธฐ

    • ๋ฆฌ์•กํŠธ ์ฟผ๋ฆฌ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์†์„ฑ ์ค‘ ๋กœ๋”ฉ๊ณผ ์—๋Ÿฌ ์ƒํƒœ๋ฅผ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š” isLoading, isError ๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
      const {data, isLoading, isError} = useQuery(...);
      if(isLoading) return <div>๋กœ๋”ฉ ํ™”๋ฉด</div>
      if(isError) return <div>์˜ค๋ฅ˜ ํ™”๋ฉด</div>
  • React์—์„œ์˜ ์—๋Ÿฌ ํ•ธ๋“ค๋ง

    • ๋ณดํ†ต api ํ˜ธ์ถœ์—” try ~ catch๋ฌธ์„ ํ†ตํ•ด ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์€ ํ•˜๋‚˜์˜ ์ปดํฌ๋„ŒํŠธ ์•ˆ์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๊ฒฝ์šฐ์™€ ์•„๋‹Œ ๊ฒฝ์šฐ์˜ ์ฝ”๋“œ๋ฅผ ๊ฐ๊ฐ ์ž‘์„ฑํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง„๋‹ค.
    • ErrorBoundary
      • React 16 ๋ฒ„์ „๋ถ€ํ„ฐ ๊ฐœ๋… ๋„์ž…
      • ErrorBoundary ์ปดํฌ๋„ŒํŠธ ํ˜•ํƒœ
        class ErrorBoundary extends React.Component {
          constructor(props) {
            super(props);
            // ์ปดํฌ๋„ŒํŠธ์˜ ์ดˆ๊ธฐ ์ƒํƒœ ์„ค์ •
            this.state = { hasError: false };
          }
        
          // ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ
          static getDerivedStateFromError(error) {
            // ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜์—ฌ ์—๋Ÿฌ ๋ฐœ์ƒ ์—ฌ๋ถ€๋ฅผ ํ‘œ์‹œ
            return { hasError: true };
          }
        
          // componentDidCatch()๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ ํ›„ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ
          componentDidCatch(error, errorInfo) {
            // ์—๋Ÿฌ ์ •๋ณด์™€ ํ•จ๊ป˜ ์„œ๋น„์Šค์— ์—๋Ÿฌ ๋กœ๊ทธ๋ฅผ ๊ธฐ๋กํ•˜๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ
            logErrorToMyService(error, errorInfo);
          }
        
          render() {
            // ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒฝ์šฐ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ํ‘œ์‹œ
            if (this.state.hasError) {
              return <h1>Something went wrong.</h1>;
            }
        
            // ์•„๋‹ˆ๋ฉด ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ Œ๋”๋ง
            return this.props.children;
          }
        }
        ์ถœ์ฒ˜: https://lasbe.tistory.com/183 [LasBe's Upgrade:ํ‹ฐ์Šคํ† ๋ฆฌ]
      • getDerivedStateFromError(error)
        • ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ๋ฅผ ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๊ฐ์ง€
        • ์—๋Ÿฌ ๋ฐœ์ƒ ์—ฌ๋ถ€๋ฅผ ์ปดํฌ๋„ŒํŠธ ์ƒํƒœ์— ๋ฐ˜์˜ํ•˜์—ฌ ์—…๋ฐ์ดํŠธ (hasError)
      • ์ƒ์œ„์—์„œ ์—๋Ÿฌ ํ•ธ๋“ค๋ง์„ ์„ ์–ธํ•˜์—ฌ ํ•˜์œ„์ปดํฌ๋„ŒํŠธ์— ์ „๋‹ฌํ•˜๋ฉด, ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ๋ณ„๋„๋กœ ๋‹ค๋ฃจ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

๐Ÿต Data Fetching Library

๊ธฐ์กด์˜ Client State ๊ด€๋ฆฌ๋Š”

  • Redux
  • React Context API
  • Recoil

๋“ฑ์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ†ตํ•ด ๊ด€๋ฆฌํ•œ๋‹ค.


Client์—์„œ API์™€ ํ†ต์‹ ์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ถ”๊ฐ€์ ์œผ๋กœ useEffect, useState๋ฅผ ํ•จ๊ป˜ ์“ฐ๋ฉด์„œ ๋ฐ์ดํ„ฐ๋ฅผ fetching ํ•ด์™”๋‹ค.

ํ•˜์ง€๋งŒ ๋ฐ์ดํ„ฐ ํŽ˜์นญ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ด๋Ÿฌํ•œ ํ›…๋“ค์˜ ํ•„์š”์„ฑ์ด ์‚ฌ๋ผ์ง„๋‹ค.

๐Ÿต React Query

React์—์„œ ๋ฐ์ดํ„ฐ fetching๊ณผ ์บ์‹ฑ ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ฐ„์†Œํ™”ํ•ด์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

  • ์™ธ๋ถ€(API ๋“ฑ)๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ fetching ๋ฐ ์—…๋ฐ์ดํŠธ ๊ณผ์ • ๊ฐ„์†Œํ™”
  • API ์š”์ฒญ์˜ ๋กœ๋”ฉ ๋ฐ ์˜ค๋ฅ˜ ์ƒํƒœ ๊ด€๋ฆฌ
  • ์บ์‹ฑ ์ž๋™ ๊ด€๋ฆฌ

React Query ์ฃผ์š” ๊ฐœ๋…

Query

  • API ์—”๋“œํฌ์ธํŠธ, DB ๋“ฑ์˜ ์›๊ฒฉ ๋ฐ์ดํ„ฐ ์†Œ์Šค๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ ์š”์ฒญ
  • useQuery ํ›… ์‚ฌ์šฉ

Mutation

  • ์„œ๋ฒ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์—…๋ฐ์ดํŠธ
  • useMutation ํ›… ์‚ฌ์šฉ

Query Caching

  • Query ๊ฒฐ๊ณผ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ Query Invalidation
  • ์ฟผ๋ฆฌ๋ฅผ ์˜ค๋ž˜๋œ ์ƒํƒœ๋กœ ์—ฌ๊ฒจ ๋ฌดํšจํ™”

1๏ธโƒฃ useQuery ํ›…

๋ฐ์ดํ„ฐ Fetching ์šฉ

API ์—”๋“œํฌ์ธํŠธ๋‚˜ DB์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋น„๋™๊ธฐ์ ์œผ๋กœ ๊ฐ€์ ธ์˜ค๋„๋ก ์„œ๋ฒ„์— ์š”์ฒญํ•˜๋Š” ๊ฒƒ


์‚ฌ์šฉ๋ฒ•

const query = useQuery( { queryKey: [ โ€˜keyโ€™ ],   queryFn: callback })

queryKey

  • ์ฟผ๋ฆฌ์˜ ๊ณ ์œ  ํ‚ค
  • React Query ์ตœ์‹  ๋ฒ„์ „๋ถ€ํ„ฐ๋Š” ๋ฐฐ์—ด ํ‘œ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•ด์„œ ํ‚ค ์ง€์ •

queryFn

  • ํ›… ํ˜ธ์ถœ ์‹œ ์‹คํ–‰๋˜๋Š” Promise ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
  • ํ•ด๋‹น callbackํ•จ์ˆ˜์—์„œ ๋ฐ์ดํ„ฐ fetching
// ์˜ˆ์‹œ
const getProducts = () => fetch( '<https://jsonplaceholder.typicode.com/users>')
                          .then( res  => res.json() )

const query = useQuery( { queryKey: [โ€˜usersโ€™],   queryFn: getTodos })

๋ฐ˜ํ™˜๊ฐ’ ์ฟผ๋ฆฌ์˜ ์ƒํƒœ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๊ฐ์ฒด

const { isLoading, isError, data, error, refetch, remove } = useQuery( { queryKey: [โ€˜todosโ€™], queryFn: getTodos });

๋Œ€ํ‘œ์ ์ธ ํ”„๋กœํผํ‹ฐ

  • isLoading ์ฟผ๋ฆฌ๊ฐ€ ํ˜„์žฌ ๋กœ๋”ฉ ์ค‘์ธ์ง€ ์—ฌ๋ถ€ (boolean)
  • isError ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๊ฐ€ ์˜ค๋ฅ˜์ธ์ง€ ์—ฌ๋ถ€ (boolean)
  • data ์ฟผ๋ฆฌ๋ฅผ ํ†ตํ•ด ์„ฑ๊ณต์ ์œผ๋กœ fetching๋œ ๋ฐ์ดํ„ฐ
  • error ์ฟผ๋ฆฌ ์‹คํ–‰ ์ค‘ ๋ฐœ์ƒํ•œ ๋ชจ๋“  ์—๋Ÿฌ
  • refetch ์ฟผ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜๋™์œผ๋กœ refetch ํ•˜๋Š” ํŠธ๋ฆฌ๊ฑฐ ๋ฉ”์†Œ๋“œ
  • remove ์บ์‹œ์—์„œ ํŠน์ • ์ฟผ๋ฆฌ ์ œ๊ฑฐํ•˜๋Š” ๋ฉ”์†Œ๋“œ

๋ฐ์ดํ„ฐ refetching

useQuery๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ

  • ์ปดํฌ๋„ŒํŠธ ์ตœ์ดˆ mount ์‹œ์— ๋ฐ์ดํ„ฐ๋ฅผ fetchingํ•ด์˜ค๊ณ ,
  • ์ดํ›„์˜ ๋ฐ์ดํ„ฐ ์—…๋ฐ์ดํŠธ์— ๋Œ€ํ•ด์„œ๋Š” ์ž๋™ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค. (๋ฐ์ดํ„ฐ refetching ์•ˆํ•ด์คŒ)

[ useQuery ์˜ต์…˜ ]

์˜ต์…˜ ์—ญํ•  ๊ธฐ๋ณธ๊ฐ’
refetchOnWindowFocus ๋ฐ์ดํ„ฐ๊ฐ€ ์˜ค๋ž˜๋œ(stale) ๊ฒฝ์šฐ ๋ธŒ๋ผ์šฐ์ € ์ฐฝ์ด ํฌ์ปค์Šค ๋˜๋ฉด ๋ฆฌํŽ˜์น˜ true
refetchOnMount ๋ฐ์ดํ„ฐ๊ฐ€ ์˜ค๋ž˜๋œ ๊ฒฝ์šฐ ๋งˆ์šดํŠธ ์‹œ ๋ฆฌํŽ˜์น˜ true
refetchOnReconnect ๋ฐ์ดํ„ฐ๊ฐ€ ์˜ค๋ž˜๋œ ๊ฒฝ์šฐ ์žฌ์—ฐ๊ฒฐ ์‹œ ๋ฆฌํŽ˜์น˜ true

ํ•˜์ง€๋งŒ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ idle ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•  ๋•Œ์—”, ๋ฐ์ดํ„ฐ ์—…๋ฐ์ดํŠธ๋ฅผ ๊ฐ์ง€ํ•˜์—ฌ ์ž๋™์œผ๋กœ ๋ฆฌํŽ˜์น˜ํ•ด์ฃผ์ง€ ์•Š๋Š”๋‹ค.

์ด๋ฅผ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด์„œ refetchIntervel ์˜ต์…˜์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ์„ค์ •ํ•œ ๋ฐ€๋ฆฌ์ดˆ ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ณ„์† refetching ํ•ด์˜ด

2๏ธโƒฃ useMutation ํ›…

API ์—”๋“œํฌ์ธํŠธ๋‚˜ DB์— ๋ฐ์ดํ„ฐ๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑ, ์ˆ˜์ •, ์‚ญ์ œ

์‚ฌ์šฉ๋ฒ•

const mutation = useMutation({ mutationFn: mutationFunction });

mutationFn

  • ํ•„์ˆ˜ ์˜ต์…˜.
  • ์‹คํ–‰ํ•˜๊ณ ์ž ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
  • ์˜ˆ์‹œ
    const AddUser = useMutation({
        mutationFn: ( user ) => {
    
          return fetch('<https://jsonplaceholder.typicode.com/users>',
          {
            method:'post',
            headers: {
               "Content-Type": "application/json",
          },
          body:JSON.stringify( user )
        }).then( res =>  res.json() )
      })

๋ฐ˜ํ™˜๊ฐ’ ๋ฎคํ…Œ์ด์…˜ ์‹คํ–‰ ๊ฒฐ๊ณผ์™€ ๊ด€๋ จ๋œ ์ •๋ณด๋ฅผ ๋‹ด์€ ๊ฐ์ฒด

๋Œ€ํ‘œ์ ์ธ ํ”„๋กœํผํ‹ฐ

  • isLoading ๋ฎคํ…Œ์ด์…˜์ด ํ˜„์žฌ ๋กœ๋”ฉ ์ค‘์ธ์ง€ ์—ฌ๋ถ€ (boolean)
  • isSuccess ๋ฎคํ…Œ์ด์…˜ ๊ฒฐ๊ณผ๊ฐ€ ์„ฑ๊ณต์ธ์ง€ ์—ฌ๋ถ€ (boolean)
  • isError ๋ฎคํ…Œ์ด์…˜ ๊ฒฐ๊ณผ๊ฐ€ ์˜ค๋ฅ˜์ธ์ง€ ์—ฌ๋ถ€ (boolean)
  • data ๋ฎคํ…Œ์ด์…˜ ์‹คํ–‰ ํ›„ ๋ฐ˜ํ™˜๋œ ๋ฐ์ดํ„ฐ (์žˆ์„ ๋•Œ๋งŒ)
  • mutate ํ•ด๋‹น ๋ฎคํ…Œ์ด์…˜ ์‹คํ–‰์„ ์œ„ํ•ด ํ˜ธ์ถœํ•  ๋ฉ”์†Œ๋“œ. (์ธ์ž์— ๋‹ด์„ ๋ณ€์ˆ˜๋ฅผ ๊ฐ์ฒด๋กœ ์ „๋‹ฌ)
  • reset ๋ฎคํ…Œ์ด์…˜ ์ดˆ๊ธฐ ์ƒํƒœ๋กœ ๋ฆฌ์…‹ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
  • onSuccess ๋ฎคํ…Œ์ด์…˜ ์„ฑ๊ณต์‹œ ํ˜ธ์ถœํ•  ์ฝœ๋ฐฑํ•จ์ˆ˜
  • onError ๋ฎคํ…Œ์ด์…˜ ์‹คํŒจ์‹œ ํ˜ธ์ถœํ•  ์ฝœ๋ฐฑํ•จ์ˆ˜

3๏ธโƒฃ Query Caching

์›๊ฒฉ ์„œ๋ฒ„์™€ ํ†ต์‹ ํ•˜๋Š”๋ฐ์— ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ ๋‹จ์ถ•ํ•˜๊ณ ์ž,

์—ฌ๋Ÿฌ๋ฒˆ ๋ถˆ๋Ÿฌ์˜ฌ ๋ฐ์ดํ„ฐ๋Š” ์บ์‹œ์— ์ €์žฅํ•˜์—ฌ ๋ฐ˜๋ณตํ•ด์„œ ํ•„์š”๋กœ ํ•  ๊ฒฝ์šฐ ์›๊ฒฉ ์„œ๋ฒ„๊ฐ€ ์•„๋‹Œ ์บ์‹œ์—์„œ ๋น ๋ฅด๊ฒŒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ์‹

useQuery ํ›… ์‚ฌ์šฉ ์‹œ, ์ง€์ •ํ–ˆ๋˜ ๊ณ ์œ  ํ‚ค ๋ฐ‘์— ๋ฐ˜ํ™˜๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์บ์‹ฑ๋œ๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” ์บ์‹œ ๋ฐ์ดํ„ฐ๋Š” ์˜ค๋ž˜๋œ(stale) ์ƒํƒœ๋กœ ๊ธฐ๋ก๋œ๋‹ค.


[ Query Caching๊ณผ ๊ด€๋ จ๋œ useQuery ์˜ต์…˜ ]

const query = useQuery({
    queryKey: [โ€˜usersโ€™],
    queryFn: getUsers,
    staleTime: 5000,
    cacheTime: 60000
  })

staleTime

  • xx๋ฐ€๋ฆฌ์ดˆ ํ›„์— ๋ฐ์ดํ„ฐ๋Š” stale ์ƒํƒœ๋กœ ์ฒ˜๋ฆฌ๋œ๋‹ค. cacheTime
  • xx๋ฐ€๋ฆฌ์ดˆ ํ›„์— ์บ์‹œ ๋ฐ์ดํ„ฐ๋Š” ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜์œผ๋กœ ๊ฐ„๋‹ค. (๋ฒ„๋ ค์ง)

4๏ธโƒฃ Query Invalidation

์ง€์ •ํ•ด์ค€ staleTime ๊ณผ ๋ฌด๊ด€ํ•˜๊ฒŒ ์ฆ‰์‹œ ๋ฐ์ดํ„ฐ๋ฅผ stale ์ƒํƒœ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, API์— POST ์š”์ฒญ์„ ๋ณด๋‚ด ๋ฐ์ดํ„ฐ ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•  ๋•Œ์—”, API ์—”๋“œํฌ์ธํŠธ์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ์˜ ๊ฐ’์ด ์บ์‹œ์— ์ €์žฅ๋˜์–ด์žˆ๋Š” ๊ฐ’๋ณด๋‹ค ๋” ์ตœ์‹  ์ƒํƒœ๊ฐ€ ๋˜๊ณ , ์บ์‹œ์— ์ €์žฅ๋˜์–ด์žˆ๋Š” ๋ฐ์ดํ„ฐ๋Š” ๊ณง๋ฐ”๋กœ outdatedํ•œ ๊ฐ’์ด ๋˜์–ด๋ฒ„๋ฆฐ๋‹ค. ์ด๋Ÿด ๋•Œ์—” ์ฆ‰์‹œ ์บ์‹œ ๋ฐ์ดํ„ฐ๋ฅผ stale ์ƒํƒœ๋กœ ์ฒ˜๋ฆฌํ•ด์ค˜์•ผ ํ•œ๋‹ค.

React Query์˜ QueryClient ๊ฐ์ฒด์˜ invalidateQueries ๋ฉ”์†Œ๋“œ ํ™œ์šฉ

  • ๋ชจ๋“  ์ฟผ๋ฆฌ, ํ˜น์€ ๊ณ ์œ  ํ‚ค๋ฅผ ํ†ตํ•ด ํŠน์ • ์ฟผ๋ฆฌ๋ฅผ stale ์ƒํƒœ๋กœ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š” ์—ญํ• 

์‚ฌ์šฉ๋ฒ•

  • useQueryClient ํ›…์„ ํ†ตํ•ด queryClient ๊ฐ์ฒด ์ƒ์„ฑ
  • ๊ฐ์ฒด ๋‚ด์žฅ ๋ฉ”์†Œ๋“œ invalidateQueries ํ™œ์šฉํ•˜๊ธฐ
import { useQueryClient } from "@tanstack/react-query";

// useQueryClient ํ›…์„ ์‚ฌ์šฉํ•˜์—ฌ queryClient ๊ฐ์ฒด ์ƒ์„ฑ
const queryClient = useQueryClient();

// ์บ์‹œ์˜ ๋ชจ๋“  ์ฟผ๋ฆฌ ๋ฌดํšจํ™”
queryClient.invalidateQueries();

// 'users'๋กœ ์‹œ์ž‘ํ•˜๋Š” ํ‚ค๊ฐ€ ์žˆ๋Š” ๋ชจ๋“  ์ฟผ๋ฆฌ ๋ฌดํšจํ™”
queryClient.invalidateQueries({ queryKey: ["users"] });


๐Ÿต ํŒจ์นญ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์“ฐ๋Š” ์ด์œ ๋Š” ๋ฌด์—‡์ผ๊นŒ?

  • React ํ›…์„ ์‚ฌ์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ„ํŽธํ•˜๊ฒŒ Fetch/Update ํ•  ์ˆ˜ ์žˆ๋‹ค!
  • ์Šค๋งˆํŠธ ์บ์‹ฑ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ํ†ตํ•ด ์„ฑ๋Šฅ์„ ์ตœ์ ํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค!!

์ •๋ฆฌํ•˜์ž๋ฉด, ์ฝ”๋“œ ๊ฐ„์†Œํ™” & ์„ฑ๋Šฅ ์ตœ์ ํ™”๋ฅผ ์œ„ํ•ด Fetching Library๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค