@convex-dev/react-query

中文文档
0.1.0发布于 2025/11/20Apache-2.0TypeScriptESM + CJS零依赖近 30 天下载 86万

Instead of polling you subscribe to receive update from server-side query functions in a Convex deployment. Convex is a database with server-side (db-side? like stored procedures) functions that update reactively.

原始使用文档

@convex-dev/react-query

Instead of polling you subscribe to receive update from server-side query functions in a Convex deployment. Convex is a database with server-side (db-side? like stored procedures) functions that update reactively.

New results for all relevant subscriptions are pushed to the client where they update at the same time so data is never stale and there's no need to call queryClient.invalidateQueries().

Setup

See ./src/example.tsx for a real example. The general pattern:

  1. Create a ConvexClient and ConvexQueryClient. Set the global default queryKeyHashFn to convexQueryClient.hashFn() and queryFn to convexQueryClient.queryFn(). Connect the ConvexQueryClient to the React Query QueryClient.
const convexClient = new ConvexReactClient(import.meta.env.VITE_CONVEX_URL);
const convexQueryClient = new ConvexQueryClient(convexClient);
const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      queryKeyHashFn: convexQueryClient.hashFn(),
      queryFn: convexQueryClient.queryFn(),
    },
  },
});
convexQueryClient.connect(queryClient);
  1. Use useQuery() with the convexQuery options factory function called with an api object imported from ../convex/_generated/server and the arguments for this query function. These two form the query key.
const { isPending, error, data } = useQuery({
  ...convexQuery(api.repos.get, { repo: "made/up" }),
  gcTime: 10000, // unsubscribe after 10s of no use
});

staleTime is set to Infinity beacuse this data is never stale; it's proactively updated whenever the query result updates on the server. (see tkdodo's post for more about this) If you like, customize the gcTime to the length of time a query subscription should remain active after all useQuery() hooks using it have unmounted.

If you need to use a Convex Action as a query, it won't be reactive; you'll get all the normal tools from React Query to refetch it.

Differences from using TanStack Query with fetch

New query results are pushed from the server, so a staleTime of Infinity should be used.

Your app will remain subscribed to a query until the gcTime has elapsed. Tune this for your app: it's usually a good tradeoff to use a value of at least a couple seconds.

Example

To run this example:

  • npm install
  • npm run dev

Mutations and Actions

If you wrap your app in a ConvexProvider you'll be able to use convex hooks like useConvexMutation and useConvexAction.:

<ConvexProvider client={convex}>
  <QueryClientProvider client={queryClient}>
    <App />
  </QueryClientProvider>
</ConvexProvider>

You can use this mutation function directly or wrap it in a TanStack Query useMutation:

const mutationFn = useConvexMutation(api.board.createColumn);
const { mutate } = useMutation({ mutationFn });
const { mutate } = useMutation({
  mutationFn: useConvexAction(api.time.getTotal),
});

Authentication

Note: The example app includes a basic Convex Auth implementation for reference.

TanStack Query isn't opionated about auth; an auth code might be a an element of a query key like any other. With Convex it's not necessary to add an additional key for an auth code; auth is an implicit argument to all Convex queries and these queries will be retried when authentication info changes.

Convex auth is typically done via JWT: some query functions will fail if requested before calling convexReactClinet.setAuth() with a function that provides the token.

Auth setup looks just like it's recommended in Convex docs, which make use of components that use native convex hooks. For Clerk, this might look like this: a ClerkProvider for auth, a ConvexProviderWithClerk for the convex client, and a QueryClient.

<ClerkProvider publishableKey="pk_test_...">
  <ConvexProviderWithClerk client={convex} useAuth={useAuth}>
    <QueryClientProvider client={queryClient}>
      <App />
    </QueryClientProvider>
  </ConvexProviderWithClerk>
</ClerkProvider>

See the Convex Auth docs for setup instructions.

TODO

  • auth
  • paginated queries
  • cleanup / unsubscribe in useEffect; something with hot reloading may not be working right

Contributing

After cloning this repo run npm i to install dependencies. This package uses tshy to publish an ESM. If there's ever demand for a CJS build we can add "cjs" to the "dialects" section of "tshy" config in the package.json.

To publish an alpha release, update the version in package.json to something like 0.0.0-alpha.1 and run just rush-pnpm publish --tag alpha.

To publish a regular release, update the version in package.json to something like 0.1.2 and run just rush-pnpm publish.

版本与发布记录

@convex-dev/react-query 共发布 10 个版本,首次发布于 2024/07/19;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.1.0latest2025/11/2011 个月前
0.0.0-alpha.112025/06/171.4 年前
0.0.0-alpha.102025/06/171.4 年前
0.0.0-alpha.92025/06/171.4 年前
0.0.0-alpha.82024/10/292 年前
0.0.0-alpha.72024/10/242 年前

其他发布通道:alpha → 0.0.0-alpha.4。

关于 @convex-dev/react-query 的常见问题

@convex-dev/react-query 是什么?

Instead of polling you subscribe to receive update from server-side query functions in a Convex deployment. Convex is a database with server-side (db-side? like stored procedures) functions that update reactively.

如何安装 @convex-dev/react-query?

在项目目录运行 npm install @convex-dev/react-query,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:convex(^1.29.3)、@tanstack/react-query(^5.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@convex-dev/react-query 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@convex-dev/react-query 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@convex-dev/react-query 可以商用吗?

@convex-dev/react-query 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@convex-dev/react-query 的下载量有多少?

根据 npm 官方下载统计,@convex-dev/react-query 在 2026/10/11 – 2026/11/09 这 30 天内被下载 860,304 次,日均约 28,677 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@convex-dev/react-query 还在维护吗?

最新版本 0.1.0 发布于 2025/11/20(11 个月前);至今共发布 10 个版本,其中过去 12 个月发布了 1 个;首次发布于 2024/07/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。