trpc-tanstack-nextjs
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesetRPC + TanStack Query + Next.js App Router
tRPC + TanStack Query + Next.js App Router
End-to-end typesafe APIs for Next.js using tRPC v11 with adapter.
@trpc/tanstack-react-query使用tRPC v11和适配器,为Next.js打造端到端类型安全的API。
@trpc/tanstack-react-queryCore Setup
核心配置
- - Full setup from scratch with all modules
setup - - Creating routers, procedures, middleware
routers - - Queries, mutations, useUtils in client components
client-usage - - Prefetching, hydration, getCaller in server components
server-usage
- - 从零开始完整配置所有模块
setup - - 创建路由、过程、中间件
routers - - 在客户端组件中使用查询、变更、useUtils
client-usage - - 在服务器组件中进行预获取、水合、getCaller调用
server-usage
Optional Integrations
可选集成
- - Add session/user to tRPC context with Better Auth
better-auth-integration - - Update UI before server confirms
optimistic-updates - - Cursor-based pagination
infinite-queries - - WebSocket real-time updates
subscriptions
- - 通过Better Auth将会话/用户信息添加到tRPC上下文
better-auth-integration - - 在服务器确认前更新UI
optimistic-updates - - 基于游标分页
infinite-queries - - WebSocket实时更新
subscriptions
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
references/setup.md
references/routers.md
references/client-usage.mdEach reference file contains:
- Code examples with imports
- Usage patterns
- Common variations
阅读各个参考文件获取详细说明和代码示例:
references/setup.md
references/routers.md
references/client-usage.md每个参考文件包含:
- 带导入语句的代码示例
- 使用模式
- 常见变体
Common Gotchas
常见问题
- Cookies not sent - Add to httpBatchLink fetch
credentials: "include" - Hydration mismatch - Ensure superjson transformer on both client and server
- staleTime: 0 - Causes refetch on every mount; use 30s+ for most cases
- Missing HydrateClient - Prefetched data won't transfer to client
- cache() not used - getCaller/getQueryClient must be wrapped in React cache()
- Cookie未发送 - 在httpBatchLink的fetch配置中添加
credentials: "include" - 水合不匹配 - 确保客户端和服务器端都使用superjson转换器
- staleTime: 0 - 会导致每次挂载时重新获取数据;大多数场景建议设置为30秒以上
- 缺少HydrateClient - 预获取的数据无法传递到客户端
- 未使用cache() - getCaller/getQueryClient必须包裹在React的cache()中