全局配置
¥Global Configuration
上下文 SWRConfig 可以为所有 SWR 钩子提供全局配置(options)。
¥The context SWRConfig can provide global configurations (options) for all SWR hooks.
<SWRConfig value={options}>
<Component/>
</SWRConfig>在此示例中,所有 SWR 钩子将使用提供的相同 fetcher 来加载 JSON 数据,并且默认情况下每 3 秒刷新一次:
¥In this example, all SWR hooks will use the same fetcher provided to load JSON data, and refresh every 3 seconds by default:
import useSWR, { SWRConfig } from 'swr'
function Dashboard () {
const { data: events } = useSWR('/api/events')
const { data: projects } = useSWR('/api/projects')
const { data: user } = useSWR('/api/user', { refreshInterval: 0 }) // override
// ...
}
function App () {
return (
<SWRConfig
value={{
refreshInterval: 3000,
fetcher: (resource, init) => fetch(resource, init).then(res => res.json())
}}
>
<Dashboard />
</SWRConfig>
)
}嵌套配置
¥Nesting Configurations
SWRConfig 合并来自父上下文的配置。它可以接收对象或功能配置。函数式接收父配置作为参数并返回一个你可以自定义的新配置。
¥SWRConfig merges the configuration from the parent context. It can receive either an object or a functional configuration. The functional one receives the parent configuration as argument and returns a new configuration that you can customize yourself.
对象配置示例
¥Object Configuration Example
import { SWRConfig, useSWRConfig } from 'swr'
function App() {
return (
<SWRConfig
value={{
dedupingInterval: 100,
refreshInterval: 100,
fallback: { a: 1, b: 1 },
}}
>
<SWRConfig
value={{
dedupingInterval: 200, // will override the parent value since the value is primitive
fallback: { a: 2, c: 2 }, // will merge with the parent value since the value is a mergeable object
}}
>
<Page />
</SWRConfig>
</SWRConfig>
)
}
function Page() {
const config = useSWRConfig()
// {
// dedupingInterval: 200,
// refreshInterval: 100,
// fallback: { a: 2, b: 1, c: 2 },
// }
}函数配置示例
¥Functional Configuration Example
import { SWRConfig, useSWRConfig } from 'swr'
function App() {
return (
<SWRConfig
value={{
dedupingInterval: 100,
refreshInterval: 100,
fallback: { a: 1, b: 1 },
}}
>
<SWRConfig
value={parent => ({
dedupingInterval: parent.dedupingInterval * 5,
fallback: { a: 2, c: 2 },
})}
>
<Page />
</SWRConfig>
</SWRConfig>
)
}
function Page() {
const config = useSWRConfig()
// {
// dedupingInterval: 500,
// fallback: { a: 2, c: 2 },
// }
}额外的 API
¥Extra APIs
缓存提供者
¥Cache Provider
除了列出的所有 options 之外,SWRConfig 还接受可选的 provider 功能。请参阅 缓存 部分了解更多详细信息。
¥Besides all the options listed, SWRConfig also accepts an optional provider function. Please refer to the Cache section for more details.
<SWRConfig value={{ provider: () => new Map() }}>
<Dashboard />
</SWRConfig>访问全局配置
¥Access To Global Configurations
你可以使用 useSWRConfig 钩子来获取全局配置,以及 mutate 和 cache:
¥You can use the useSWRConfig hook to get the global configurations, as well as mutate and cache:
import { useSWRConfig } from 'swr'
function Component () {
const { refreshInterval, mutate, cache, ...restConfig } = useSWRConfig()
// ...
}嵌套配置将得到扩展。如果没有使用 <SWRConfig>,则返回默认值。
¥Nested configurations will be extended. If no <SWRConfig> is used, it will return the default ones.