'use client'; // Toast 通知系统:右上角弹出,自动消失(替代 alert) // 用法:const { toast } = useToast(); toast('发布成功', 'success'); import { createContext, useContext, useState, useCallback, useRef } from 'react'; import { createPortal } from 'react-dom'; const ToastContext = createContext(null); export function useToast() { return useContext(ToastContext); } let idCounter = 0; export function ToastProvider({ children }) { const [toasts, setToasts] = useState([]); const timersRef = useRef({}); const toast = useCallback((message, type = 'success', duration = 3000) => { const id = ++idCounter; setToasts((prev) => [...prev, { id, message, type }]); if (timersRef.current[id]) clearTimeout(timersRef.current[id]); timersRef.current[id] = setTimeout(() => { setToasts((prev) => prev.filter((t) => t.id !== id)); delete timersRef.current[id]; }, duration); }, []); const dismiss = useCallback((id) => { setToasts((prev) => prev.filter((t) => t.id !== id)); if (timersRef.current[id]) { clearTimeout(timersRef.current[id]); delete timersRef.current[id]; } }, []); return ( {children} {typeof document !== 'undefined' && createPortal(
{toasts.map((t) => (
dismiss(t.id)}> {t.message}
))}
, document.body )}
); }