import { useState } from 'react' /** * 弹框通知 * @returns */ const useNotification = () => { const [message, setMessage] = useState('') const [isVisible, setIsVisible] = useState(false) const showNotification = msg => { setMessage(msg) setIsVisible(true) setTimeout(() => { closeNotification() }, 3000) } const closeNotification = () => { setIsVisible(false) setMessage('') } // 测试通知效果 // const toggleVisible = () => { // setIsVisible(prev => !prev) // 使用函数式更新 // } // useEffect(() => { // document?.addEventListener('click', toggleVisible) // return () => { // document?.removeEventListener('click', toggleVisible) // } // }, []) /** * 通知组件 * @returns */ const Notification = () => { return (
{message}
) } return { showNotification, closeNotification, Notification } } export default useNotification