使用React怎么实现全局组件Toast轻提示效果
本篇文章为大家展示了使用React怎么实现全局组件Toast轻提示效果,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
创新互联公司主要从事网站建设、成都网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务克什克腾,十余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220
需求分析
Toast 不需要同页面一起被渲染,而是根据需要被随时调用。
Toast 是一个轻量级的提示组件,它的提示不会打断用户操作,并且会在提示的一段时间后自动关闭。
Toast 需要提供几种不同的消息类型以适应不同的使用场景。
Toast 的方法必须足够简洁,以避免不必要的代码冗余。
如何使用
首先引入
import Toast from './components/toast'
JSX中事件调用:
JS中方法调用:
Toast.info('普通提示')
回调方法:
const hideLoading = Toast.loading('加载中...', 0, () => { Toast.success('加载完成') }) setTimeout(hideLoading, 2000)
调用规则:
3个参数:
content 提示内容 string(loading方法为可选)
duration 提示持续时间 number,单位ms(可选)
onClose 提示关闭时的回调函数(可选)
Toast.info("普通",2000) Toast.success("成功",1000,() => { console.log('回调方法') })) Toast.error("错误") Toast.loading()
代码实现
目录结构:
index.js:对外export接口,设置默认的参数值,全局创建或销毁Toast的DIV。
toast.js:Toast具体显示的内容及多次调用Toast时的状态管理。
toast.css:Toast的样式,费话不多说。
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(, 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;//仅展示最后一个提示 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 ({ notices.map(notice => (
{notice.content}