Skip to content
开发文档
API

API

const { data, error, isLoading, isValidating, mutate } = useSWR(key, fetcher, options)

参数

¥Parameters

  • key:请求的唯一键字符串(或函数/数组/空)(详情)(高级用法)

    ¥key: a unique key string for the request (or a function / array / null) (details), (advanced usage)

  • fetcher:(可选)一个 Promise 返回函数来获取你的数据 (详情)

    ¥fetcher: (optional) a Promise-returning function to fetch your data (details)

  • options:(可选)此 SWR 钩子的选项对象

    ¥options: (optional) an object of options for this SWR hook

返回值

¥Return Values

  • data:由 fetcher 解析的给定键的数据(如果未加载则为未定义)

    ¥data: data for the given key resolved by fetcher (or undefined if not loaded)

  • errorfetcher 抛出的错误(或未定义)

    ¥error: error thrown by fetcher (or undefined)

  • isLoading:如果有正在进行的请求并且没有 "加载的数据"。回退数据和之前的数据不被考虑 "加载的数据"

    ¥isLoading: if there's an ongoing request and no "loaded data". Fallback data and previous data are not considered "loaded data"

  • isValidating:如果有请求或重新验证加载

    ¥isValidating: if there's a request or revalidation loading

  • mutate(data?, options?):改变缓存数据 (详情) 的函数

    ¥mutate(data?, options?): function to mutate the cached data (details)

更多信息可参见 此处

¥More information can be found here.

选项

¥Options

  • suspense = false:启用 React Suspense 模式 (详情)

    ¥suspense = false: enable React Suspense mode (details)

  • fetcher(args):获取器函数

    ¥fetcher(args): the fetcher function

  • revalidateIfStale = true:即使存在旧数据也会自动重新验证 (详情)

    ¥revalidateIfStale = true: automatically revalidate even if there is stale data (details)

  • revalidateOnMount:安装组件时启用或禁用自动重新验证 (详情)

    ¥revalidateOnMount: enable or disable automatic revalidation when component is mounted (details)

  • revalidateOnFocus = true:当窗口获得焦点时自动重新验证 (详情)

    ¥revalidateOnFocus = true: automatically revalidate when window gets focused (details)

  • revalidateOnReconnect = true:当浏览器重新获得网络连接时自动重新验证(通过 navigator.onLine(详情)

    ¥revalidateOnReconnect = true: automatically revalidate when the browser regains a network connection (via navigator.onLine) (details)

  • refreshInterval (详情)

    ¥refreshInterval (details):

    • 默认禁用:refreshInterval = 0

      ¥Disabled by default: refreshInterval = 0

    • 如果设置为数字,则轮询间隔以毫秒为单位

      ¥If set to a number, polling interval in milliseconds

    • 如果设置为函数,该函数将接收最新数据并应返回以毫秒为单位的间隔

      ¥If set to a function, the function will receive the latest data and should return the interval in milliseconds

  • refreshWhenHidden = false:当窗口不可见时进行轮询(如果启用了 refreshInterval

    ¥refreshWhenHidden = false: polling when the window is invisible (if refreshInterval is enabled)

  • refreshWhenOffline = false:浏览器离线时轮询(由 navigator.onLine 决定)

    ¥refreshWhenOffline = false: polling when the browser is offline (determined by navigator.onLine)

  • shouldRetryOnError = true:当 fetcher 出错时重试

    ¥shouldRetryOnError = true: retry when fetcher has an error

  • dedupingInterval = 2000:在此时间范围内具有相同键的数据去重请求(以毫秒为单位)

    ¥dedupingInterval = 2000: dedupe requests with the same key in this time span in milliseconds

  • focusThrottleInterval = 5000:在以毫秒为单位的时间范围内仅重新验证一次

    ¥focusThrottleInterval = 5000: only revalidate once during a time span in milliseconds

  • loadingTimeout = 3000:触发 onLoadingSlow 事件的超时时间(以毫秒为单位)

    ¥loadingTimeout = 3000: timeout to trigger the onLoadingSlow event in milliseconds

  • errorRetryInterval = 5000:错误重试间隔(以毫秒为单位)

    ¥errorRetryInterval = 5000: error retry interval in milliseconds

  • errorRetryCount:最大错误重试次数

    ¥errorRetryCount: max error retry count

  • fallback:多个后备数据 (例子) 的键值对象

    ¥fallback: a key-value object of multiple fallback data (example)

  • fallbackData:要返回的初始数据(注意:这是每个钩子)

    ¥fallbackData: initial data to be returned (note: This is per-hook)

  • keepPreviousData = false:返回前一个键的数据,直到新数据加载完毕 (详情)

    ¥keepPreviousData = false: return the previous key's data until the new data has been loaded (details)

  • onLoadingSlow(key, config):当请求加载时间过长时的回调函数(参见 loadingTimeout

    ¥onLoadingSlow(key, config): callback function when a request takes too long to load (see loadingTimeout)

  • onSuccess(data, key, config):请求成功完成时的回调函数

    ¥onSuccess(data, key, config): callback function when a request finishes successfully

  • onError(err, key, config):请求返回错误时的回调函数

    ¥onError(err, key, config): callback function when a request returns an error

  • onErrorRetry(err, key, config, revalidate, revalidateOps):错误重试处理程序

    ¥onErrorRetry(err, key, config, revalidate, revalidateOps): handler for error retry

  • onDiscarded(key):由于竞争条件而忽略请求时的回调函数

    ¥onDiscarded(key): callback function when a request is ignored due to race conditions

  • compare(a, b):比较函数用于检测返回的数据何时发生更改,以避免虚假重新渲染。默认情况下,使用 stable-hash (opens in a new tab)

    ¥compare(a, b): comparison function used to detect when returned data has changed, to avoid spurious rerenders. By default, stable-hash (opens in a new tab) is used.

  • isPaused():函数检测是否暂停重新验证,返回 true 时将忽略获取的数据和错误。默认返回 false

    ¥isPaused(): function to detect whether pause revalidations, will ignore fetched data and errors when it returns true. Returns false by default.

  • use:中间件函数数组 (详情)

    ¥use: array of middleware functions (details)

💡

在慢速网络(2G、<= 70Kbps)下,errorRetryInterval 默认为 10 秒, loadingTimeout 默认为 5 秒。

¥When under a slow network (2G, <= 70Kbps), errorRetryInterval will be 10s, and loadingTimeout will be 5s by default.

你还可以使用 全局配置 提供默认选项。

¥You can also use global configuration to provide default options.