React實(shí)現(xiàn)全局組件的Toast輕提示效果
Toast是常用的輕提示彈框,常用于頁(yè)面loading和提示語(yǔ)彈窗。
本例基于React實(shí)現(xiàn)一個(gè)隨時(shí)可調(diào)用且不隨頁(yè)面渲染的全局組件。
需求分析
- Toast 不需要同頁(yè)面一起被渲染,而是根據(jù)需要被隨時(shí)調(diào)用。
- Toast 是一個(gè)輕量級(jí)的提示組件,它的提示不會(huì)打斷用戶操作,并且會(huì)在提示的一段時(shí)間后自動(dòng)關(guān)閉。
- Toast 需要提供幾種不同的消息類型以適應(yīng)不同的使用場(chǎng)景。
- Toast 的方法必須足夠簡(jiǎn)潔,以避免不必要的代碼冗余。
如何使用
首先引入
import Toast from './components/toast'
JSX中事件調(diào)用:
<button onClick={() => { Toast.info('普通提示') }}>普通提示</button>
JS中方法調(diào)用:
Toast.info('普通提示')
回調(diào)方法:
const hideLoading = Toast.loading('加載中...', 0, () => {
Toast.success('加載完成')
})
setTimeout(hideLoading, 2000)
調(diào)用規(guī)則:
3個(gè)參數(shù):
- content 提示內(nèi)容 string(loading方法為可選)
- duration 提示持續(xù)時(shí)間 number,單位ms(可選)
- onClose 提示關(guān)閉時(shí)的回調(diào)函數(shù)(可選)
Toast.info("普通",2000)
Toast.success("成功",1000,() => {
console.log('回調(diào)方法')
}))
Toast.error("錯(cuò)誤")
Toast.loading()
代碼實(shí)現(xiàn)
目錄結(jié)構(gòu):

- index.js:對(duì)外export接口,設(shè)置默認(rèn)的參數(shù)值,全局創(chuàng)建或銷毀Toast的DIV。
- toast.js:Toast具體顯示的內(nèi)容及多次調(diào)用Toast時(shí)的狀態(tài)管理。
- toast.css:Toast的樣式,費(fèi)話不多說(shuō)。
index.js:
import React from 'react'
import ReactDOM from 'react-dom'
import Toast from './toast'
import './toast.css'
function createNotification() {
const div = document.createElement('div')
document.body.appendChild(div)
const notification = ReactDOM.render(<Toast />, 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, content, duration, onClose })
}
export default {
info(content, duration, onClose) {
return notice('info', content, duration, onClose)
},
success(content = '操作成功', duration, onClose) {
return notice('success', content, duration, onClose)
},
error(content, duration , onClose) {
return notice('error', content, duration, onClose)
},
loading(content = '加載中...', duration = 0, onClose) {
return notice('loading', content, duration, onClose)
}
}
toast.js:
import React, { Component } from 'react'
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;//僅展示最后一個(gè)提示
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 => (
<div className="toast_bg" key={notice.key}>
<div className='toast_box'>
<div className={`toast_icon ${icons[notice.type]}`}></div>
<div className='toast_text'>{notice.content}</div>
</div>
</div>
))
}
</div>
)
}
}
export default ToastBox
toast.css:
.toast {
position: fixed;
left: 0;
top: 0;
z-index: 999;
display: flex;
flex-direction: column; }
.toast_bg {
position: fixed;
width: 100%;
height: 100%;
left: 0;
top: 0; }
.toast_box {
position: relative;
left: 50%;
top: 50%;
width: 2.8rem;
height: 2rem;
margin: -1rem -1.4rem;
background: rgba(0, 0, 0, 0.65);
border-radius: .1rem;
color: #fff; }
.toast_text {
position: absolute;
bottom: 16%;
text-align: center;
width: 90%;
margin: 0 5%;
height: .28rem;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap; }
.toast_icon {
position: relative;
left: 50%;
top: 15%;
margin: -.4rem;
width: .8rem;
height: .8rem; }
.toast_loading {
-webkit-animation: loading 1s steps(12, end) infinite;
animation: loading 1s steps(12, end) infinite;
background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAAM1BMVEUAAAD///////////////////////////////////////////////////////////////+3leKCAAAAEHRSTlMAENCA8KAgsGDgQMCQUDBwhylaLQAAAL1JREFUOMu9U0kSwyAMK9jsS/T/1zZt2pgEZzq9RBeMZYRGDI/70bO5JptjrOAQVTonIJVK5bW2ma9A7VvpK8OdeQfbZectrDnyU+Oo0b68wGK0muDPdxpOciaizq5pkAgiIPAoew2rBVNYZoM2YHbZDNKz/2Ogam3ff5gMEL8wisfh2KKZiFiGWFkk1B7NSbhNQFy4M2+PghbODNsg7y8THM2njiy8gBgcaEUA9GgNJbxh6fJv+NxiFvYmPAFtCQZNK1qZIAAAAABJRU5ErkJggg==") no-repeat;
background-size: 100%; }
.toast_success {
background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAAM1BMVEUAAAD///////////////////////////////////////////////////////////////+3leKCAAAAEHRSTlMA8DAQ0GBP4LCggMBwIJBAIttdjAAAAINJREFUOMvdkUsOwyAMBbH5hUCauf9pK1SlohF438x2LPn52f09+8vUfiNb/gighj8FouEjYCUoQDXiBSD7pdcMiK7XC9wCFmlDO3T20Scgx287ne13pwDU89NOJ3g3maCmJDANqIGRtLj8oi1ed1GMdmcB7wXIYX8QdQZJiM5Em3smbyVICDCOrCqSAAAAAElFTkSuQmCC") no-repeat;
background-size: 100%; }
.toast_error {
background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgBAMAAACBVGfHAAAAMFBMVEUAAAD///////////////////////////////////////////////////////////87TQQwAAAAD3RSTlMA0BDAMODwUKBgsCCAQJClzVPvAAAA0UlEQVQoz2MgErAclv9o44Dgc8b/B4KvBTA+t/3XdgeWivjPG6ACbl8ngNXlp0AN+L8IwtD6DzFm2w+Y3v5sMGW/ACbA9Rms9ZsCTIApH2QR608GhoUKQJ4xA8P8AKCAP5CwF2JgUPwIlPwCFDj/AMRRYJIHCnL8AZkJ1AfkAcUYGNhBpso7MICUgBQw8H4EEv/B5ssDFYA4mAKYWjANfd+Aai3CYZ9BDoM63RDkdEGQ0zE9h+l9zADCDEIGt2/wQEZEwwVepGhgYEdEFGZUEgYAW05XI3jSsVwAAAAASUVORK5CYII=") no-repeat;
background-size: 100%; }
.toast_info {
background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgBAMAAACBVGfHAAAALVBMVEUAAAD///////////////////////////////////////////////////////+hSKubAAAADnRSTlMA4CCAwKBAMJBg8NAQUNhWlbcAAAC+SURBVCjPYyASsLfse+1cgOBzyr0DgocXYHwmv4dtCkwZck8UoAJZDydA1C2H8NnexUAYR99BjNF6CtMbtwhM+QUACUUhIMH6BKz14QEgafcYSPDIgSxifMkAE2CYJwAk6gQQAozPgURfA0KAA0T6JSAE2ECm7lNACDC9BhLvGGACIA6GAFyLohBEC9xQqLeeQKwFA4i1EIfBAeNzuNMVhSBOx/AcpvcxAwgzCDEDGTMaGHhhEYWIShN4VBIGAPvRT5YzufhUAAAAAElFTkSuQmCC") no-repeat;
background-size: 100%; }
@-webkit-keyframes loading {
0% {
-webkit-transform: rotate3d(0, 0, 1, 0deg);
transform: rotate3d(0, 0, 1, 0deg); }
100% {
-webkit-transform: rotate3d(0, 0, 1, 360deg);
transform: rotate3d(0, 0, 1, 360deg); } }
@keyframes loading {
0% {
-webkit-transform: rotate3d(0, 0, 1, 0deg);
transform: rotate3d(0, 0, 1, 0deg); }
100% {
-webkit-transform: rotate3d(0, 0, 1, 360deg);
transform: rotate3d(0, 0, 1, 360deg); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
react?card?slider實(shí)現(xiàn)滑動(dòng)卡片教程示例
這篇文章主要為大家介紹了react?card?slider實(shí)現(xiàn)滑動(dòng)卡片教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
react-redux多個(gè)組件數(shù)據(jù)共享的方法
這篇文章主要介紹了react-redux多個(gè)組件數(shù)據(jù)共享的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
解決react-connect中使用forwardRef遇到的問(wèn)題
這篇文章主要介紹了解決react-connect中使用forwardRef遇到的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
簡(jiǎn)單的React SSR服務(wù)器渲染實(shí)現(xiàn)
這篇文章主要介紹了簡(jiǎn)單的React SSR服務(wù)器渲染實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
React項(xiàng)目build打包頁(yè)面空白的解決方案
React項(xiàng)目執(zhí)行build命令后,在本地服務(wù)器打開(kāi)頁(yè)面是空白的,本文主要介紹了React項(xiàng)目build打包頁(yè)面空白的解決方案,感興趣的可以了解一下2023-08-08
React為什么需要Scheduler調(diào)度器原理詳解
這篇文章主要為大家介紹了React為什么需要Scheduler調(diào)度器原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10

