본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Toast 마이그레이션 가이드 (v2 → v3)

원문 보기 위키 갱신

이 문서는 HeroUI의 Toast 컴포넌트를 v2에서 v3로 옮길 때 알아야 할 변경 사항을 정리한 마이그레이션 가이드예요. provider 패턴과 훅이 전역 함수로 바뀌고, 변형 이름도 일부 달라졌어요.

출처: HeroUI

본문

참고: 완전한 API 레퍼런스, 스타일링 가이드, 고급 예제는 v3 Toast 문서를 참고하세요. 이 가이드는 HeroUI v2에서 마이그레이션하는 데만 집중한 내용이에요.

구조 변경 (Structure Changes)

v2에서 Toast는 provider와 훅 패턴을 사용했어요.

import { ToastProvider, useToast } from "@heroui/react";

function App() {
  return (
    <ToastProvider>
      <MyComponent />
    </ToastProvider>
  );
}

function MyComponent() {
  const { toast } = useToast();

  return (
    <button onClick={() => toast.show("Hello!")}>
      Show Toast
    </button>
  );
}

v3에서 Toast는 provider 컴포넌트와 전역 toast() 함수를 사용해요.

import { Toast } from "@heroui/react";

function App() {
  return (
    <>
      <Toast.Provider />
      <MyComponent />
    </>
  );
}

function MyComponent() {
  return (
    <button onClick={() => toast("Hello!")}>
      Show Toast
    </button>
  );
}

핵심 변경 사항 (Key Changes)

1. Provider 패턴 (Provider Pattern)

v2: ToastProvider 래퍼 필요
v3: Toast.Provider 컴포넌트 사용 (어디에나 배치 가능)

2. 훅 → 함수 (Hook → Function)

v2: useToast() 훅 사용
v3: toast() 함수 직접 사용

3. API 변경 (API Changes)

v2: toast.show() 메서드 사용
v3: toast()는 헬퍼 메서드(toast.success(), toast.danger() 등)를 가진 함수예요.

4. 변형 이름 (Variant Names)

v2: success, error, warning, info 같은 변형
v3: 변형: default, accent, success, warning, danger

5. 컴파운드 컴포넌트 구조 (Compound Component Structure)

v3: Toast는 커스텀 렌더링을 위해 컴파운드 컴포넌트를 사용해요.

  • Toast — 메인 toast 컨테이너
  • Toast.Content — 콘텐츠 래퍼
  • Toast.Title — 제목 텍스트
  • Toast.Description — 설명 텍스트
  • Toast.Indicator — 아이콘/인디케이터
  • Toast.CloseButton — 닫기 버튼
  • Toast.ActionButton — 액션 버튼

6. Promise 지원 (Promise Support)

v3: 비동기 작업 처리를 위한 toast.promise() 내장 promise 지원

마이그레이션 예제 (Migration Examples)

제목과 설명이 있는 Toast (Toast with Title and Description)

const { toast } = useToast();

toast.show({
  title: "Success",
  description: "Your changes have been saved",
  variant: "success"
});
import { toast } from "@heroui/react";

toast.success("Success", {
  description: "Your changes have been saved"
});

변형용 헬퍼 메서드 (Helper Methods for Variants)

const { toast } = useToast();

toast.show({ variant: "success", title: "Success" });
toast.show({ variant: "error", title: "Error" });
toast.show({ variant: "warning", title: "Warning" });
toast.show({ variant: "info", title: "Info" });
import { toast } from "@heroui/react";

toast.success("Success");
toast.danger("Error");
toast.warning("Warning");
toast.info("Info");

Promise 지원 (Promise Support)

const { toast } = useToast();

const handleAsync = async () => {
  try {
    await someAsyncOperation();
    toast.show({ title: "Success", variant: "success" });
  } catch {
    toast.show({ title: "Error", variant: "error" });
  }
};
import { toast } from "@heroui/react";

const handleAsync = async () => {
  toast.promise(someAsyncOperation(), {
    loading: "Processing...",
    success: "Operation completed!",
    error: "Operation failed"
  });
};

커스텀 Toast 렌더링 (Custom Toast Rendering)

const { toast } = useToast();

toast.show({
  title: "Custom",
  render: (toast) => (
    <div>Custom content</div>
  )
});
import { Toast, ToastContent, ToastTitle } from "@heroui/react";

<Toast.Provider>
  {({ toast: toastItem }) => (
    <Toast toast={toastItem}>
      <ToastContent>
        <ToastTitle>Custom content</ToastTitle>
      </ToastContent>
    </Toast>
  )}
</Toast.Provider>

요약 (Summary)

  • ToastProvider를 Toast.Provider로 교체하세요.
  • useToast() 훅을 toast() 함수로 교체하세요.
  • 변형 이름을 업데이트하세요 (error → danger, info → accent).
  • 헬퍼 메서드(toast.success(), toast.danger() 등)를 사용하세요.
  • 비동기 작업에는 toast.promise()를 사용하세요.
  • 커스텀 렌더링을 위한 컴파운드 컴포넌트 구조.
  • 더 나은 TypeScript 지원과 큐 관리.

더 알아보기 (Learn more)