react实现简单的toast组件

实现:(这里使用的是tsx,如果是jsx,需要把类型删除)

  1. 生成一个新的div,添加(append)到document下,然后使用ReactDOM渲染该元素
  2. 创建一个Toast的类,因为需要类直接使用方法,所以创建相应的静态方法,直接调用
  3. 在方法调用前,清除定时器(如同节流),防止前面的定时器影响到后面弹出的toast的持续时间

使用:

function handleClick1() {
    Toast.success("发送成功")
}
function handleClick2() {
    Toast.success("请求成功")
}
function handleClick3() {
    Toast.info("登录成功")
}
function handleClick4() {
    Toast.fail("请求失败")
}
function handleClick5() {
    Toast.warning("请求失败")
}
function handleClick6() {
    Toast.loading("加载中", true)
    setTimeout(() => {
        Toast.loading("加载中", false)
    }, 3000)
}

Toast组件 :success,info,fail,warning几种形式,可以自定义

import * as React from 'react'
import './index.scss'
import * as ReactDOM from 'react-dom'
import {Fragment} from "react"
let timer: any = null
class Toast extends React.Component {
    static info(msg:string | "info", timeout: number = 2000) {
        init()
        setTime(timeout)
        ReactDOM.render(<Fragment>
            <span>{msg}</span>
        </Fragment>, document.getElementById('dark-toast'));
    }
    static success(msg:string | "success", timeout: number = 2000) {
        init()
        setTime(timeout)
        ReactDOM.render(<Fragment>
            <i className="iconfont icon-check-circle-fill"></i>
            <span>{msg}</span>
        </Fragment>, document.getElementById('dark-toast'));
    }
    static fail(msg:string | "fail", timeout: number = 2000) {
        init()
        setTime(timeout)
        ReactDOM.render(<Fragment>
            <i className="iconfont icon-close-circle-fill"></i>
            <span>{msg}</span>
        </Fragment>, document.getElementById('dark-toast'));
    }
    static warning(msg:string | "warning", timeout: number = 2000) {
        init()
        setTime(timeout)
        ReactDOM.render(<Fragment>
            <i className="iconfont icon-warning-circle-fill"></i>
            <span>{msg}</span>
        </Fragment>, document.getElementById('dark-toast'));
    }
    static loading(msg:string | "loading", status: boolean) {
        init()
        setLoading(status)
        ReactDOM.render(<Fragment>
            <i className="iconfont icon-reload rotate-loop"></i>
            <span>{msg}</span>
        </Fragment>, document.getElementById('dark-toast'));
    }
}
function setLoading(status: boolean) {
    let dark_toast:any = document.getElementById('dark-toast')
    if (status)
        dark_toast.style.display = "block"
    else
        dark_toast.style.display = "none"
}
function init () {
    clearTimeout(timer)
    let dark_toast = document.getElementById('dark-toast')
    if (dark_toast) {
        dark_toast.style.display = "block"
    } else {
        let div = document.createElement("div")
        div.setAttribute("id", "dark-toast")
        document.body.appendChild(div);
    }
}
function setTime (timeout:number) {
    timer = setTimeout (() => {
        let dark_toast = document.getElementById('dark-toast')
        if (dark_toast) {
            dark_toast.style.display = "none"
        }
    }, timeout)
}
export default Toast



pointer-events: none;  使该元素能够点击穿透,不挡住下面的元素 

#dark-toast {
  position: fixed;
  top: 40%;
  left: 50%;
  transform: translateX(-50%);
  max-width: 40%;
  background: #333;
  z-index: 1000;
  padding: 5px 10px;
  background: rgba(29, 29, 29, 0.73);
  border-radius: 5px;
  color: #fff;
  text-align: center;
  min-width: 100px;
  pointer-events: none; // 点击穿透
  i {
    font-size: 40px;
    display: block;
    &.rotate-loop {
      animation: rotate-loop 1s infinite;
    }
  }
}
@keyframes rotate-loop {
  from {
    transform: rotateZ(0deg);
  }
  to {
    transform: rotateZ(360deg);
  }
}

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单React封装toast组件的示例: ```jsx import React, { useState, useEffect } from 'react'; import ReactDOM from 'react-dom'; import './toast.css'; const Toast = ({ message, duration = 3000, onClose }) => { const [visible, setVisible] = useState(false); useEffect(() => { setVisible(true); const timer = setTimeout(() => { setVisible(false); onClose && onClose(); }, duration); return () => clearTimeout(timer); }, [duration, onClose]); return visible ? ( <div className="toast"> <div className="toast-message">{message}</div> </div> ) : null; }; const showToast = (message, duration = 3000, onClose) => { const div = document.createElement('div'); document.body.appendChild(div); ReactDOM.render( <Toast message={message} duration={duration} onClose={() => { ReactDOM.unmountComponentAtNode(div); onClose && onClose(); }} />, div ); }; export default showToast; ``` 这个组件包含一个`Toast`组件和一个`showToast`函数。`Toast`组件接收`message`、`duration`和`onClose`作为属性,其中`message`表示要显示的消息,`duration`表示消息显示的时间(默认为3秒),`onClose`表示关闭消息时的回调函数。 `showToast`函数用于在页面上创建一个`Toast`组件并显示消息。它接收与`Toast`组件相同的属性,还有一个可选的`onClose`回调函数,用于在消息关闭时执行其他操作。 在使用时,可以像这样调用`showToast`函数: ```jsx import React from 'react'; import showToast from './toast'; const MyComponent = () => { const handleClick = () => { showToast('Hello, world!', 2000, () => console.log('Toast closed.')); }; return ( <button onClick={handleClick}>Show Toast</button> ); }; export default MyComponent; ``` 这个示例在点击按钮时显示一个消息框,显示2秒后关闭,并在关闭时输出一条消息到控制台。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值