Skip to content
Yenowme edited this page Dec 18, 2021 · 1 revision

1. ๊ฐœ์š”

  • api ํ†ต์‹  ๊ธฐ๋ฐ˜ ์ „์—ญ ์ƒํƒœ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ.

  • SWR์€ ์บ์‹œ๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ ํ›„, fetch ์š”์ฒญ(์žฌ๊ฒ€์ฆ)์„ ํ•˜๊ณ , ์ตœ์ข…์ ์œผ๋กœ ์ตœ์‹ ํ™”๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ „๋žต

  • useSWR ํ•จ์ˆ˜๋Š” ์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ ํ†ต์‹  ์ฃผ์†Œ, ํˆฌ๋ฒˆ์งธ ์ธ์ž๋กœ ํ†ต์‹  ํ•จ์ˆ˜๋ฅผ ๋ฐ›์•„ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์ฒซ๋ฒˆ์งธ ๊ฐ์ฒด๋กœ, ์—๋Ÿฌ๋ฅผ ๋‘๋ฒˆ์งธ ๊ฐ์ฒด๋กœ ์ „๋‹ฌํ•จ.

    const {data, error, isValidating, mutate} = useSWR(key, fetcher, options)
    • params
      • key : ์‹๋ณ„์ž, api ์ฃผ์†Œ๊ฐ€ ์ฃผ๋กœ ๋“ค์–ด๊ฐ
      • fetcher : Promise data๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
      • option : swr ์˜ต์…˜ ์„ค์ • ๊ฐ์ฒด
    • return value
      • data : fetcher๋กœ๋ถ€ํ„ฐ ๋ฐ˜ํ™˜๋œ ๋ฐ์ดํ„ฐ
      • error : fetcher ์—๋Ÿฌ ๊ฐ์ฒด
      • isValidating : boolean fetching ์ค‘์ผ๋•Œ ?
      • mutate : ์บ์‹ฑ ๋ฐ์ดํ„ฐ ์—…๋ฐ์ดํŠธ ํ•จ์ˆ˜

2. ์„ค์น˜

yarn add swr
or 
npm i swr

3. useSWR

  1. ์ฝœ๋ฐฑ ์ธ์ž๋กœ ์‚ฌ์šฉํ•  fetcher ํ•จ์ˆ˜๋ฅผ ์ œ์ž‘ํ•œ๋‹ค

    const fetcher = (url) => axios.get(url).then((res) => res.data);
  2. useSWR ์„ ์ž„ํฌํŠธ ํ•˜๊ณ  ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

    import useSWR from 'swr'
    const fetcher = (url) => axios.get(url).then((res) => res.data);
    
    function Profile() {
      const { data, error } = useSWR('/api/user', fetcher)
    
      if (error) return <div>failed to load</div>
      if (!data) return <div>loading...</div>
      return <div>hello {data.name}!</div>
    }
  3. ์ปค์Šคํ…€ ํ›…์„ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค.

    function useUser (id) {
      const { data, error } = useSWR(`/api/user/${id}`, fetcher)
    
      return {
        user: data,
        isLoading: !error && !data,
        isError: error
      }
    }
    • ์ปค์Šคํ…€ ํ›…์„ ์ œ์ž‘ํ•ด์„œ state๋ฅผ props์— ์ „๋‹ฌํ•  ํ•„์š” ์—†์ด ํ•„์š”ํ•œ ๊ณณ์—์„œ ๋ชจ๋‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ์—ฌ๋Ÿฌ๋ฒˆ ์žฌ์‚ฌ์šฉํ•ด๋„ ์š”์ฒญ์€ ํ•œ๋ฒˆ์œผ๋กœ ํ†ต์ผ๋œ๋‹ค.

4. mutate

import useSWR, { useSWRConfig } from 'swr'

const App () => {

const { mutate } = useSWRConfig()
mutate('/api') //ํ•ด๋‹น ํ‚ค๋ฅผ ๊ฐ€์ง„ swr์— ๊ฐฑ์‹  ์š”์ฒญ

///...return
}
  • ์บ์‹œ๋ฅผ ์—…๋ฐ์ดํŠธ ํ•  ๋•Œ ์‚ฌ์šฉ.
  • mutate(key) ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋™์ผํ•œ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋‹ค๋ฅธSWR hook์—๊ฒŒ ๊ฐฑ์‹  ๋ฉ”์‹œ์ง€๋ฅผ ์ „์—ญ์œผ๋กœ ๋ธŒ๋กœ๋“œ์บ์ŠคํŒ…ํ•  ์ˆ˜ ์žˆ๋‹ค.

ํฌ์ŠคํŠธ ์š”์ฒญ ์‹œ

  • ๊ธฐ๋ณธ์ ์œผ๋กœ swr์€ get์— ํŠนํ™”๋˜์–ด์žˆ๋‹ค. ๋”ฐ๋ผ์„œ post๋งŒ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ํฐ ์ด์ ์ด ์—†๋‹ค๊ณ  ํ•œ๋‹ค.
  • ํ•˜์ง€๋งŒ get์œผ๋กœ ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ์—…๋ฐ์ดํŠธ ํ•  ๋• mutate๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํšจ๊ณผ์ ์ด๋‹ค
mutate(key, postData, false)
  • ํฌ์ŠคํŠธ ์š”์ฒญ์„ ๋ณด๋‚ผ ๋•Œ, ์„œ๋ฒ„์—์„œ ๋ฐ›์•„์˜ค์ง€ ์•Š๊ณ  ๋กœ์ปฌ์„ ๋จผ์ € ์—…๋ฐ์ดํŠธ ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Œ (์„ธ๋ฒˆ์งธ ์ธ์ž๋ฅผ false)๋กœ ๋‘๋ฉด ๊ฐ€๋Šฅ.

  • useSWR์— ๋ฐ˜ํ™˜๋œ mutate๋กœ key๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ

    import useSWR from 'swr'
    
    function Profile () {
      const { data, mutate } = useSWR('/api/user', fetcher)
    
      return (
        <div>
          <h1>My name is {data.name}.</h1>
          <button onClick={async () => {
            const newName = data.name.toUpperCase()
            // ๋ฐ์ดํ„ฐ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์œ„ํ•ด API๋กœ ์š”์ฒญ์„ ์ „์†ก
            await requestUpdateUsername(newName)
            // ๋กœ์ปฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ฆ‰์‹œ ์—…๋ฐ์ดํŠธํ•˜๊ณ  ๊ฐฑ์‹ (refetch)
            // ๋…ธํŠธ: ๋ฏธ๋ฆฌ ๋ฐ”์ธ๋”ฉ ๋˜์—ˆ์œผ๋ฏ€๋กœ useSWR์˜ ๋ฎคํ…Œ์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” key๊ฐ€ ์š”๊ตฌ๋˜์ง€ ์•Š์Œ
            mutate({ ...data, name: newName })
          }}>Uppercase my name!</button>
        </div>
      )
    }
  • ๐Ÿšฉ post api ์™€ get api๊ฐ€ ๋‹ค๋ฅผ ๋•Œ

    1. mutate๋ฅผ false๋กœ ํ•œ ๋’ค ๋กœ์ปฌ ๋ฐ์ดํ„ฐ๋งŒ ์—…๋ฐ์ดํŠธ
    2. axios์š”์ฒญ์œผ๋กœ ๋ฐ์ดํ„ฐ post
    3. ๋‹ค์‹œ mutate์‹คํ–‰ํ•ด์„œ ์‹ฑํฌ ์—…๋ฐ์ดํŠธ

5.๋ฐ์ดํ„ฐ ์ž๋™ ๊ฐฑ์‹ 

  • ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‹ค์‹œ ์‚ฌ์ดํŠธ๊ฐ€ ํฌ์ปค์‹ฑ๋˜๋ฉด ์บ์‹œ๋ฅผ ์—…๋ฐ์ดํŠธํ•จ

  • ์˜ต์…˜์— refreshInterval ์„ ์„ค์ •ํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Œ

    useSWR('/api/todos', fetcher, { refreshInterval: 1000 })

์ฐธ๊ณ 

Clone this wiki locally