๐ŸŒ ์บ์‹œ = ์ฑ…์ƒ ์œ„์˜ ๋ฐ”๋‚˜๋‚˜. ๋ฐ˜์‘ ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ ๋ฐ˜์‘ ์„ฑ๋Šฅ

9824 ๋‹จ์–ด reactbeginnersfrontendjavascript

๐Ÿค” ๋ฆฌ์•กํŠธ ์ฟผ๋ฆฌ๋ž€?



๋ฆฌ์•กํŠธ ์บ์‹œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
Official site

๐Ÿค” ์บ์‹œ๋ž€?





์ง€๊ธˆ ์ง‘์—์„œ ์ผํ•˜๊ณ  ๋ถ€์—Œ์— ๊ฐ€์„œ ๋ฐ”๋‚˜๋‚˜๋ฅผ ๋จน๋Š”๋‹ค๊ณ  ์ƒ์ƒํ•ด ๋ณด์‹ญ์‹œ์˜ค. 1์‹œ๊ฐ„์ด ์ง€๋‚˜๋ฉด ๋ฐฐ๊ฐ€ ๊ณ ํŒŒ์ง€๊ณ  ๋˜ ๋ถ€์—Œ์— ๊ฐ€๊ณ ...

์ฑ…์ƒ ์œ„์— ๋ฐ”๋‚˜๋‚˜ ์˜ฌ๋ ค๋†“๊ณ  ๊ฐ€์ ธ๊ฐ€์„ธ์š”! ๋ถ€์—Œ์— ๋ช‡ ๋ฒˆ์ด๊ณ  ๊ฐˆ ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

Bananas = data
kitchen = server
your desk = cache


(1000๊ฐœ์ฒ˜๋Ÿผ ์ฑ…์ƒ์— ๋ฐ”๋‚˜๋‚˜๋ฅผ ๋„ˆ๋ฌด ๋งŽ์ด ์˜ฌ๋ ค๋†“์œผ๋ฉด ์ผ์ด ๋„ˆ๋ฌด ํž˜๋“ค์–ด์ง‘๋‹ˆ๋‹ค. ์กฐ์‹ฌํ•˜์„ธ์š”)

โฌ ๋„ค, ์ž์„ธํžˆ ์•Œ๋ ค๋“œ๋ฆด๊ฒŒ์š” โฌ

๐Ÿ˜ข ๊ณ ์ „์ ์ธ ๋ฐฉ์‹์˜ ์•ฝ์  ์ƒํƒœ ๊ด€๋ฆฌ



์„œ๋ฒ„ ๋ฐ React ์•ฑ ์ƒํƒœ์˜ ๋ฐ์ดํ„ฐ ํ†ตํ•ฉ
(์˜ˆ: Redux "์Šคํ† ์–ด")

๋ฐ”๋‚˜๋‚˜์™€ ๋…ธํŠธ์™€ ํŽœ๊ณผ ๋‹ค๋ฅธ ๋ชจ๋“  ๊ฒƒ๋“ค์ด ์ฑ…์ƒ ์œ„์—์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ํ˜ผ๋ž€ ์Šค๋Ÿฝ์Šต๋‹ˆ๋‹ค.

๐Ÿ˜‡ ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?



๋ณ„๋„์˜ ์ƒํƒœ ๊ด€๋ฆฌ ํ›„ "react-query"๋Š” ์บ์‹œ๋กœ ์„œ๋ฒ„ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

๋ฐ”๋‚˜๋‚˜๋ฅผ ์œ„ํ•ด ์ฑ…์ƒ ์œ„์— ์ž‘์€ ์ƒ์ž๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๐Ÿ‘ ๋ฐ˜์‘ ์ฟผ๋ฆฌ ์‚ฌ์šฉ์˜ ์ด์ 


1.๊ฐ€์ ธ์˜ค๊ธฐ ํšŸ์ˆ˜ ์ตœ์ ํ™”



์ฑ…์ƒ ์œ„์˜ ๋ฐ”๋‚˜๋‚˜๋งŒ ์ƒํ•˜๋ฉด ๋ถ€์—Œ์œผ๋กœ ๊ฐ€์•ผ ํ•œ๋‹ค

2.๋” ๋‚˜์€ UX(๋กœ๋“œ ์‹œ๊ฐ„ ๋‹จ์ถ•)



๋ถ€์—Œ์— ๊ฐ€๋ฉด ์ผ์„ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์‹œ๊ฐ„ ๋‚ญ๋น„

3. ์ ์€ ์ฝ”๋“œ



โ‡ฉํด๋ž˜์‹ ์ฝ”๋“œ

export const useClassicalFetch = () => {
  const { tasks, setTasks } = useStateContext()
  const [isLoading, setLoading] = useState(false)
  const [isError, setError] = useState(false)

  useEffect(() => {
    const fetchData = async () => {
      setError(false)
      setLoading(true)
      try {
        const res = await axios('http://hoge.com/api/tasks/')
        setTasks(res.data)
      } catch (error) {
        setError(true)
      }
      setLoading(false)
    }
    fetchData()
  }, [setTasks])

  return { tasks, isLoading, isError }
}


โ‡ฉ๋ฐ˜์‘ ์ฟผ๋ฆฌ

const getTasks = async () => {
  const { data } = await axios.get<Task[]>('http://hoge.com/api/tasks/')
  return data
}

// *** Here definition of react-query ***
export const useQueryTasks = () => {
  return useQuery<Task[], Error>({
    queryKey: 'tasks',
    queryFn: getTasks,
    cacheTime: 10000,//10sec
    staleTime: 10000,
  })
}


์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

const TaskList: VFC = () => {
  const { status, data } = useQueryTasks()
  if (status === 'loading') return <div>{'Loading...'}</div>
  if (status === 'error') return <div>{'Error'}</div>
  return (
    <div>
      {data?.map((task) => (
        // omit here
      ))}
    </div>
  )
}



๐ŸŒŸ ๋ฐ˜์‘ ์ฟผ๋ฆฌ ๊ตฌ์„ฑ ๊ฐ’ ์„ค๋ช…



staleTime





์˜ˆ๋ฅผ ๋“ค์–ด 10000ms(10์ดˆ)๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ํƒ‘์žฌํ•œ ํ›„ ์ฒ˜์Œ 10์ดˆ ๋™์•ˆ ์บ์‹œ ๋ฐ์ดํ„ฐ๋Š” "์‹ ์„ ํ•œ"์ƒํƒœ๊ฐ€ ๋˜๊ณ  ๊ทธ ํ›„์—๋Š” "๋ถ€์‹ค(์ด์ „)"๋กœ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค.

fresh -> using cache
stale -> fetch data from server


cacheTime





์˜ˆ๋ฅผ ๋“ค์–ด 10000ms(10์ดˆ)๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
๊ตฌ์„ฑ์š”์†Œ ๋งˆ์šดํŠธ ํ•ด์ œ 10์ดˆ ํ›„ ์บ์‹œ ๋ฐ์ดํ„ฐ ์‚ญ์ œ

refetchOnWindowFocus



์ปค์„œ๊ฐ€ ์ฐฝ์— ์ดˆ์ ์ด ๋งž์ถฐ์ง€๋ฉด ์ž๋™์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

// ex. When you use chrome and firefox. 
use chrome
โ†“
use firefox
โ†“
use chrome, then fetch data automatically !


์ฐธ์กฐ



this page , ์ผ๋ณธ์–ด๋กœ ๋˜์–ด ์žˆ์ง€๋งŒ

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ