import React, { useRef, useState } from 'react' import { Button, ToasterContainer, ToasterType, AnimatedToaster } from 'react-icecream' interface ToastInfo { id: number visible: boolean type: ToasterType message: string } export default function Demo() { const idxRef = useRef(0) const [toasts, setToasts] = useState([]) function addItem() { const types: ToasterType[] = ['info', 'success', 'warning', 'error'] idxRef.current++ setToasts([ ...toasts, { id: idxRef.current, visible: true, type: types[Math.ceil(Math.random() * 4)], message: '新消息来咯,类型随机。' } ]) } function hideItem() { const newToasts = toasts.slice() const item = newToasts.find(it => it.visible) if (item != null) { item.visible = false setToasts(newToasts) } } return ( <>
{toasts.map(({ id, ...toast }) => )}
) }