React实现全局组件的Toast轻提示效果

Toast是常用的轻提示弹框,常用于页面loading和提示语弹窗。

本例基于React实现一个随时可调用且不随页面渲染的全局组件。

需求分析

  • Toast 不需要同页面一起被渲染,而是根据需要被随时调用。
  • Toast 是一个轻量级的提示组件,它的提示不会打断用户操作,并且会在提示的一段时间后自动关闭。
  • Toast 需要提供几种不同的消息类型以适应不同的使用场景。
  • Toast 的方法必须足够简洁,以避免不必要的代码冗余。

如何使用

首先引入

JSX中事件调用:

{ Toast.info('普通提示') }}>普通提示

JS中方法调用:

回调方法:

{ Toast.success('加载完成') }) setTimeout(hideLoading,2000)

调用规则:

3个参数:

  • content 提示内容string(loading方法为可选)
  • duration 提示持续时间 number,单位ms(可选)
  • onClose 提示关闭时的回调函数(可选)
{ console.log('回调方法') })) Toast.error("错误") Toast.loading()

代码实现

目录结构:

  1. index.js:对外export接口,设置默认的参数值,全局创建或销毁Toast的DIV。
  2. toast.js:Toast具体显示的内容及多次调用Toast时的状态管理。
  3. toast.css:Toast的样式,费话不多说。

index.js:

function createNotification() {
const div = document.createElement('div')
document.body.appendChild(div)
const notification = ReactDOM.render(,div)
return {
addNotice(notice) {
return notification.addNotice(notice)
},destroy() {
ReactDOM.unmountComponentAtNode(div)
document.body.removeChild(div)
}
}
}

let notification
const notice = (type,content,duration = 2000,onClose) => {
if (!notification) notification = createNotification()
return notification.addNotice({ type,duration,onClose })
}

export default {
info(content,onClose) {
return notice('info',onClose)
},success(content = '操作成功',onClose) {
return notice('success',error(content,onClose) {
return notice('error',loading(content = '加载中...',duration = 0,onClose) {
return notice('loading',onClose)
}
}

toast.js:

class ToastBox extends Component {
constructor() {
super()
this.transitionTime = 300
this.state = { notices: [] }
this.removeNotice = this.removeNotice.bind(this)
}

getNoticeKey() {
const { notices } = this.state
return notice-${new Date().getTime()}-${notices.length}
}

addNotice(notice) {
const { notices } = this.state
notice.key = this.getNoticeKey()

// notices.push(notice);//展示所有的提示
notices[0] = notice;//仅展示最后一个提示

this.setState({ notices })
if (notice.duration > 0) {
  setTimeout(() => {
    this.removeNotice(notice.key)
  },notice.duration)
}
return () => { this.removeNotice(notice.key) }

}

removeNotice(key) {
const { notices } = this.state
this.setState({
notices: notices.filter((notice) => {
if (notice.key === key) {
if (notice.onClose) setTimeout(notice.onClose,this.transitionTime)
return false
}
return true
})
})
}

render() {
const { notices } = this.state
const icons = {
info: 'toast_info',success: 'toast_success',error: 'toast_error',loading: 'toast_loading'
}
return (
<div className="toast">
{
notices.map(notice => (

{notice.content}
)) }
) } }

export default ToastBox

toast.css:

@-webkit-keyframes loading {
0% {
-webkit-transform: rotate3d(0,1,0deg);
transform: rotate3d(0,0deg); }
100% {
-webkit-transform: rotate3d(0,360deg);
transform: rotate3d(0,360deg); } }

@keyframes loading {
0% {
-webkit-transform: rotate3d(0,360deg); } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


kindeditor4.x代码高亮功能默认使用的是prettify插件,prettify是Google提供的一款源代码语法高亮着色器,它提供一种简单的形式来着色HTML页面上的程序代码,实现方式如下: 首先在编辑器里面插入javascript代码: 确定后会在编辑器插入这样的代码: <pre
这一篇我将介绍如何让kindeditor4.x整合SyntaxHighlighter代码高亮,因为SyntaxHighlighter的应用非常广泛,所以将kindeditor默认的prettify替换为SyntaxHighlighter代码高亮插件 上一篇“让kindeditor显示高亮代码”中已经
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面
如何用js控制图片放大缩小
JS怎么获取当前时间戳
JS如何判断对象是否为数组
JS怎么获取图片当前宽高
JS对象如何转为json格式字符串
JS怎么获取图片原始宽高
怎么在click事件中调用多个js函数
js如何往数组中添加新元素
js如何拆分字符串
JS怎么对数组内元素进行求和
JS如何判断屏幕大小
js怎么解析json数据
js如何实时获取浏览器窗口大小
原生JS实现别踩白块小游戏(五)
原生JS实现别踩白块小游戏(一)