서스펜스
비공식 베타 번역
이 페이지는 PageTurner AI로 번역되었습니다(베타). 프로젝트 공식 승인을 받지 않았습니다. 오류를 발견하셨나요? 문제 신고 →
정보
- React 최신 버전을 사용 중인지 확인하세요
- Next.js에서 tRPC의 자동 SSR과 함께 서스펜스를 사용할 경우, 쿼리 실패 시
<ErrorBoundary />가 있어도 서버 측에서 전체 페이지가 중단됩니다
사용법
팁
useSuspenseQuery와 useSuspenseInfiniteQuery는 모두 [data, query]-튜플을 반환하여 데이터에 직접 접근하고 변수명을 설명적인 이름으로 변경하기 쉽게 합니다
useSuspenseQuery()
tsx// @filename: pages/index.tsximportReact from 'react';import {trpc } from '../utils/trpc';functionPostView () {const [post ,postQuery ] =trpc .post .byId .useSuspenseQuery ({id : '1' });return <>{/* ... */}</>;}
tsx// @filename: pages/index.tsximportReact from 'react';import {trpc } from '../utils/trpc';functionPostView () {const [post ,postQuery ] =trpc .post .byId .useSuspenseQuery ({id : '1' });return <>{/* ... */}</>;}
useSuspenseInfiniteQuery()
tsx// @filename: pages/index.tsximport React from 'react';import { trpc } from '../utils/trpc';function PostView() {const [pages, allPostsQuery] = trpc.post.all.useSuspenseInfiniteQuery({},{getNextPageParam(lastPage) {return lastPage.nextCursor;},},);const { isFetching, isFetchingNextPage, fetchNextPage, hasNextPage } =allPostsQuery;return <>{/* ... */}</>;}
tsx// @filename: pages/index.tsximport React from 'react';import { trpc } from '../utils/trpc';function PostView() {const [pages, allPostsQuery] = trpc.post.all.useSuspenseInfiniteQuery({},{getNextPageParam(lastPage) {return lastPage.nextCursor;},},);const { isFetching, isFetchingNextPage, fetchNextPage, hasNextPage } =allPostsQuery;return <>{/* ... */}</>;}