useInfiniteQuery
비공식 베타 번역
이 페이지는 PageTurner AI로 번역되었습니다(베타). 프로젝트 공식 승인을 받지 않았습니다. 오류를 발견하셨나요? 문제 신고 →
정보
- 이 훅을 사용하려면 프로시저가
string,number등 모든 타입의cursor입력을 허용해야 합니다. - 무한 쿼리에 대한 자세한 내용은 react-query 문서를 참조하세요.
- 이 예제에서는 Prisma를 사용합니다. 커서 기반 페이지네이션 문서를 확인해 보세요.
예제 프로시저
server/routers/_app.tstsximport { initTRPC } from '@trpc/server';import { z } from 'zod';import { Context } from './[trpc]';export const t = initTRPC.create();export const appRouter = t.router({infinitePosts: t.procedure.input(z.object({limit: z.number().min(1).max(100).nullish(),cursor: z.number().nullish(), // <-- "cursor" needs to exist, but can be any typedirection: z.enum(['forward', 'backward']), // optional, useful for bi-directional query}),).query(async (opts) => {const { input } = opts;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 { initTRPC } from '@trpc/server';import { z } from 'zod';import { Context } from './[trpc]';export const t = initTRPC.create();export const appRouter = t.router({infinitePosts: t.procedure.input(z.object({limit: z.number().min(1).max(100).nullish(),cursor: z.number().nullish(), // <-- "cursor" needs to exist, but can be any typedirection: z.enum(['forward', 'backward']), // optional, useful for bi-directional query}),).query(async (opts) => {const { input } = opts;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.infinitePosts.useInfiniteQuery({limit: 10,},{getNextPageParam: (lastPage) => lastPage.nextCursor,// initialCursor: 1, // <-- optional you can pass an initialCursor},);// [...]}
components/MyComponent.tsxtsximport { trpc } from '../utils/trpc';export function MyComponent() {const myQuery = trpc.infinitePosts.useInfiniteQuery({limit: 10,},{getNextPageParam: (lastPage) => lastPage.nextCursor,// initialCursor: 1, // <-- optional you can pass an initialCursor},);// [...]}
헬퍼
getInfiniteData()
이 헬퍼는 기존 무한 쿼리에서 현재 캐시된 데이터를 가져옵니다.
components/MyComponent.tsxtsximport { trpc } from '../utils/trpc';export function MyComponent() {const utils = trpc.useUtils();const myMutation = trpc.infinitePosts.add.useMutation({async onMutate(opts) {await utils.infinitePosts.cancel();const allPosts = utils.infinitePosts.getInfiniteData({ limit: 10 });// [...]},});}
components/MyComponent.tsxtsximport { trpc } from '../utils/trpc';export function MyComponent() {const utils = trpc.useUtils();const myMutation = trpc.infinitePosts.add.useMutation({async onMutate(opts) {await utils.infinitePosts.cancel();const allPosts = utils.infinitePosts.getInfiniteData({ limit: 10 });// [...]},});}
setInfiniteData()
이 헬퍼를 사용하면 쿼리의 캐시된 데이터를 업데이트할 수 있습니다.
components/MyComponent.tsxtsximport { trpc } from '../utils/trpc';export function MyComponent() {const utils = trpc.useUtils();const myMutation = trpc.infinitePosts.delete.useMutation({async onMutate(opts) {await utils.infinitePosts.cancel();utils.infinitePosts.setInfiniteData({ 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.useUtils();const myMutation = trpc.infinitePosts.delete.useMutation({async onMutate(opts) {await utils.infinitePosts.cancel();utils.infinitePosts.setInfiniteData({ limit: 10 }, (data) => {if (!data) {return {pages: [],pageParams: [],};}return {...data,pages: data.pages.map((page) => ({...page,items: page.items.filter((item) => item.status === 'published'),})),};});},});// [...]}