BFE.dev
React 代码题4. useSWR() I

4. useSWR() I

ReactHooks
分享给更多人

  -通过 / -执行

SWR is a popular library of data fetching.

Let's try to implement the basic usage by ourselves.

import React from 'react'function App() {  const { data, error } = useSWR('/api', fetcher)  if (error) return <div>failed</div>  if (!data) return <div>loading</div>  return <div>succeeded</div>}
  1. this is not to replicate the true implementation of useSWR()
  2. The first argument key is for deduplication, we can safely ignore it for now
上一个下一个

一边解释一边码字。

解答AI讨论(108)

Try it out

Console