码迷,mamicode.com
首页 > 其他好文 > 详细

react---简易toast组件

时间:2019-07-31 18:42:14      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:xtend   class   pre   this   min   toast   return   col   str   

组件核心代码:

import React, { Component } from ‘react‘
import PropTypes from ‘prop-types‘;

// toast 弹框组件
class Toast extends Component {
static defaultProps = {
msg: ‘操作成功‘, // 默认提示语
time: 2000, // 默认弹框出现到消失的时间
}

static propTypes = {
msg: PropTypes.string,
time: PropTypes.number,
}

num = 1;
componentDidMount() {
const { msg, time } = this.props;
this.showToast(msg, time);
}

showToast = (msg, duration) => {
duration = isNaN(duration) ? 2000 : duration;
var m = document.createElement(‘div‘);
m.innerHTML = msg;
m.style.cssText = "width:50%; min-width: 180px; background: #000; opacity: 0.6; height: auto; min-height: 30px; color: #fff; line-height: 30px; text-align: center; border-radius: 4px; position: fixed; top: 60%; left: 20%; z-index: 9999;"
document.body.appendChild(m);
setTimeout(function() {
var d = 0.5;
m.style.webkitTransition = ‘opacity ‘ + d + ‘s ease-in‘;
m.style.opacity = ‘0‘;
setTimeout(function() {
document.body.removeChild(m)
}, d * 1000);
}, duration);
}

render() {
return [];
}
}
export default Toast;
  

组件调用:

        <Toast
         msg="操作成功"
         time={2000}
        />

  

react---简易toast组件

标签:xtend   class   pre   this   min   toast   return   col   str   

原文地址:https://www.cnblogs.com/yxfboke/p/11278064.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!