框架
版本

broadcastQueryClient (实验性)

非常重要:此工具目前仍处于实验阶段。这意味着在次要版本和补丁版本中都可能发生重大更改。请自行承担风险使用。如果您选择在生产环境中使用此实验阶段的功能,请将版本锁定到补丁级别,以避免意外中断。

broadcastQueryClient 是一个用于在同源的浏览器标签页/窗口之间广播和同步您的 queryClient 状态的实用程序。

安装

此实用程序作为单独的包提供,并且可以在 '@tanstack/query-broadcast-client-experimental' 导入路径下找到。

用法

导入 broadcastQueryClient 函数,并将您的 QueryClient 实例传递给它,还可以选择设置一个 broadcastChannel

tsx
import { broadcastQueryClient } from '@tanstack/query-broadcast-client-experimental'

const queryClient = new QueryClient()

broadcastQueryClient({
  queryClient,
  broadcastChannel: 'my-app',
})
import { broadcastQueryClient } from '@tanstack/query-broadcast-client-experimental'

const queryClient = new QueryClient()

broadcastQueryClient({
  queryClient,
  broadcastChannel: 'my-app',
})

API

broadcastQueryClient

QueryClient 实例和可选的 broadcastChannel 传递给此函数。

tsx
broadcastQueryClient({ queryClient, broadcastChannel })
broadcastQueryClient({ queryClient, broadcastChannel })

选项

一个选项对象

tsx
interface BroadcastQueryClientOptions {
  /** The QueryClient to sync */
  queryClient: QueryClient
  /** This is the unique channel name that will be used
   * to communicate between tabs and windows */
  broadcastChannel?: string
  /** Options for the BroadcastChannel API */
  options?: BroadcastChannelOptions
}
interface BroadcastQueryClientOptions {
  /** The QueryClient to sync */
  queryClient: QueryClient
  /** This is the unique channel name that will be used
   * to communicate between tabs and windows */
  broadcastChannel?: string
  /** Options for the BroadcastChannel API */
  options?: BroadcastChannelOptions
}

默认选项为

tsx
{
  broadcastChannel = 'tanstack-query',
}
{
  broadcastChannel = 'tanstack-query',
}