React Custom Suspense

React Custom Suspense

Custom Suspense

suspense 의 구조를 이해할 수 있다.

import React from "react"; export interface SuspenseProps { fallback: React.ReactNode; } interface SuspenseState { pending: boolean; error?: any; } function isPromise(i: any): i is Promise<any> { return i && typeof i.then === "function"; } export default class Suspense extends React.Component< SuspenseProps, SuspenseState > { public state: SuspenseState = { pending: false }; public componentDidCatch(catchedPromise: any) { if (isPromise(catchedPromise)) { this.setState({ pending: true }); catchedPromise .then(() => { this.setState({ pending: false }); }) .catch((err) => { this.setState({ error: err || new Error("Suspense Error") }); }); } else { throw catchedPromise; } } public componentDidUpdate() { if (this.state.pending && this.state.error) { throw this.state.error; } } public render() { return this.state.pending ? this.props.fallback : this.props.children; } }
JP
이중표Frontend Engineer

3년차 프론트엔드 개발자. Next.js, React, TypeScript 기반 웹 애플리케이션 개발 전문. 대규모 트래픽 환경에서 SSR·ISR 렌더링 전략 설계 경험.

이력서 보기