Skip to main content
Version: 10.x

客户端概览

虽然可以像任何其他 REST API 一样使用普通 HTTP 请求来调用 tRPC API,但你需要一个客户端才能从 tRPC 的类型安全性中受益。

¥While a tRPC API can be called using normal HTTP requests like any other REST API, you will need a client to benefit from tRPC's typesafety.

客户知道 API 中可用的过程及其输入和输出。它使用此信息为你提供查询和突变的自动补齐功能,正确键入返回的数据,并在你编写与后端形状不匹配的请求时显示错误。

¥A client knows the procedures that are available in your API, and their inputs and outputs. It uses this information to give you autocomplete on your queries and mutations, correctly type the returned data, and show errors if you are writing requests that don't match the shape of your backend.

如果你使用 React,调用 tRPC API 的最佳方法是使用我们的 React 查询集成,它除了类型安全 API 调用之外还提供缓存、失效以及加载和错误状态管理。如果你将 Next.js 与 /pages 目录一起使用,则可以使用我们的 Next.js 集成,它除了 React 查询集成之外还添加了服务器端渲染和静态生成的辅助程序。

¥If you are using React, the best way to call a tRPC API is by using our React Query Integration, which in addition to typesafe API calls also offers caching, invalidation, and management of loading and error state. If you are using Next.js with the /pages directory, you can use our Next.js integration, which adds helpers for Serverside Rendering and Static Generation in addition to the React Query Integration.

如果你想从其他服务器或我们没有集成的前端框架调用 tRPC API,你可以使用 普通客户端

¥If you want to call a tRPC API from another server or from a frontend framework for which we don't have an integration, you can use the Vanilla Client.

除了 React 和 Next.js 集成以及 Vanilla Client 之外,还有各种 针对各种其他框架的社区构建的集成。请注意,这些不是由 tRPC 团队维护的。

¥In addition to the React and Next.js integrations and the Vanilla Client, there are a variety of community-built integrations for a variety of other frameworks. Please note that these are not maintained by the tRPC team.