useInfiniteQuery
非官方测试版翻译
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
信息
- 您的过程需要接受类型为
any的cursor输入 - 有关无限查询的更多详情,请阅读 react-query 文档
- 本示例使用 Prisma - 请参阅其基于游标的分页文档
示例过程
server/routers/_app.tstsximport * as trpc from '@trpc/server';import { Context } from './[trpc]';import { z } from 'zod';export const appRouter = trpc.router<Context>().query('infinitePosts', {input: z.object({limit: z.number().min(1).max(100).nullish(),cursor: z.number().nullish(), // <-- "cursor" needs to exist, but can be any type}),async resolve({ input }) {const limit = input.limit ?? 50;const { cursor } = input;const items = await prisma.post.findMany({take: limit + 1, // get an extra item at the end which we'll use as next cursorwhere: {title: {contains: 'Prisma' /* Optional filter */,},},cursor: cursor ? { myCursor: cursor } : undefined,orderBy: {myCursor: 'asc',},})let nextCursor: typeof cursor | undefined = undefined;if (items.length > limit) {const nextItem = items.pop()nextCursor = nextItem!.myCursor;}return {items,nextCursor,};})
server/routers/_app.tstsximport * as trpc from '@trpc/server';import { Context } from './[trpc]';import { z } from 'zod';export const appRouter = trpc.router<Context>().query('infinitePosts', {input: z.object({limit: z.number().min(1).max(100).nullish(),cursor: z.number().nullish(), // <-- "cursor" needs to exist, but can be any type}),async resolve({ input }) {const limit = input.limit ?? 50;const { cursor } = input;const items = await prisma.post.findMany({take: limit + 1, // get an extra item at the end which we'll use as next cursorwhere: {title: {contains: 'Prisma' /* Optional filter */,},},cursor: cursor ? { myCursor: cursor } : undefined,orderBy: {myCursor: 'asc',},})let nextCursor: typeof cursor | undefined = undefined;if (items.length > limit) {const nextItem = items.pop()nextCursor = nextItem!.myCursor;}return {items,nextCursor,};})
示例 React 组件
components/MyComponent.tsxtsximport { trpc } from '../utils/trpc';export function MyComponent() {const myQuery = trpc.useInfiniteQuery(['infinitePosts',{limit: 10,},],{getNextPageParam: (lastPage) => lastPage.nextCursor,},);// [...]}
components/MyComponent.tsxtsximport { trpc } from '../utils/trpc';export function MyComponent() {const myQuery = trpc.useInfiniteQuery(['infinitePosts',{limit: 10,},],{getNextPageParam: (lastPage) => lastPage.nextCursor,},);// [...]}
辅助函数
getInfiniteQueryData()
该辅助函数从现有无限查询中获取当前缓存的数据
components/MyComponent.tsxtsximport { trpc } from '../utils/trpc';export function MyComponent() {const utils = trpc.useContext();const myMutation = trpc.useMutation('infinitePosts.add', {onMutate({ post }) {await utils.cancelQuery(['infinitePosts']);const allPosts = utils.getInfiniteQueryData(['infinitePosts', { limit: 10 }]);// [...]}})}
components/MyComponent.tsxtsximport { trpc } from '../utils/trpc';export function MyComponent() {const utils = trpc.useContext();const myMutation = trpc.useMutation('infinitePosts.add', {onMutate({ post }) {await utils.cancelQuery(['infinitePosts']);const allPosts = utils.getInfiniteQueryData(['infinitePosts', { limit: 10 }]);// [...]}})}
setInfiniteQueryData()
该辅助函数允许您更新查询的缓存数据
components/MyComponent.tsxtsximport { trpc } from '../utils/trpc';export function MyComponent() {const utils = trpc.useContext();const myMutation = trpc.useMutation('infinitePosts.delete', {onMutate({ post }) {await utils.cancelQuery(['infinitePosts']);utils.setInfiniteQueryData(['infinitePosts', { limit: 10 }], (data) => {if (!data) {return {pages: [],pageParams: []}}return {...data,pages: data.pages.map((page) => {...page,items: page.items.filter((item) => item.status === 'published')})}});}});// [...]}
components/MyComponent.tsxtsximport { trpc } from '../utils/trpc';export function MyComponent() {const utils = trpc.useContext();const myMutation = trpc.useMutation('infinitePosts.delete', {onMutate({ post }) {await utils.cancelQuery(['infinitePosts']);utils.setInfiniteQueryData(['infinitePosts', { limit: 10 }], (data) => {if (!data) {return {pages: [],pageParams: []}}return {...data,pages: data.pages.map((page) => {...page,items: page.items.filter((item) => item.status === 'published')})}});}});// [...]}