React中怎么封装全局弹框,具体过程是怎样
Admin 2022-06-23 群英技术资讯 1058 次浏览
今天这篇给大家分享的知识是“React中怎么封装全局弹框,具体过程是怎样”,小编觉得挺不错的,对大家学习或是工作可能会有所帮助,对此分享发大家做个参考,希望这篇“React中怎么封装全局弹框,具体过程是怎样”文章能帮助大家解决问题。本文实例为大家分享了react封装全局弹框的具体代码,供大家参考,具体内容如下
弹框效果图

文件布局

index.js
/* eslint-disable react/no-render-return-value */
import React, { Component } from 'react'
import { is, fromJS } from 'immutable'
import ReactDOM from 'react-dom'
import './alert.less'
const close = require('../images/guanbi.png')
const line = require('../images/line.png')
const defaultState = {
alertStatus: false,
alertTip: null,
alertTitle: '详情',
closeAlert: () => {}
}
class Toptips extends Component {
state = {
...defaultState
}
// css动画组件设置为目标组件
FirstChild = props => {
const childrenArray = React.Children.toArray(props.children)
return childrenArray[0] || null
}
// 关闭弹框
confirm = () => {
const that = this
console.log(that)
this.setState(
{
alertStatus: false
},
() => {
that.state.closeAlert()
}
)
}
open = data => {
const options = data || {}
options.alertStatus = true
this.setState({
...defaultState,
...options
})
}
close = () => {
const that = this
that.state.closeAlert()
this.setState({
...defaultState
})
}
shouldComponentUpdate = (nextProps, nextState) => {
return (
!is(fromJS(this.props), fromJS(nextProps)) ||
!is(fromJS(this.state), fromJS(nextState))
)
}
render() {
const { alertStatus, alertTip, alertTitle } = this.state
console.log(alertTip, alertTitle)
return (
<div
className="alert-con"
style={alertStatus ? { display: 'block' } : { display: 'none' }}
>
<div className="alert-context">
<div className="alert-content-title">{alertTitle}</div>
<img className="alert-content-line" src={line} alt="line" />
<div className="alert-content-detail">{alertTip}</div>
<img
role="presentation"
onClick={() => {
this.confirm()
}}
className="alert-close"
src={close}
alt="关闭"
/>
</div>
</div>
)
}
}
const div = document.createElement('div')
const props = {}
document.body.appendChild(div)
const Box = ReactDOM.render(React.createElement(Toptips, props), div)
export default Box
less
.alert-con {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(255, 255, 255, 0.3);
z-index: 222;
}
.alert-context {
// background-color: #fff;
// border-radius: 16px;
position: relative;
// height: 500px;
height: 90%;
width: 750px;
margin: 40px auto 0;
background: url(../images/alertBJ.png) no-repeat center;
background-size: 100% 100%;
.alert-close{
width: 30px;
height: 30px;
position: absolute;
right: 30px;
top: 30px;
}
.alert-content-title{
width: 100%;
height: 80px;
line-height: 80px;
color: #fff;
text-align: center;
font-size: 36px;
font-weight: bolder;
// background: url(../images/line.png) no-repeat left bottom;
}
.alert-content-line{
width: 100%;
height: 20px;
margin-top: -44px;
margin-left: -6px;
}
.alert-detais-list{
width: 102%;
height: 100%;
overflow-y: auto;
padding: 20px 60px;
.alert-detais-list-C{
p{
&:nth-child(1){
font-size: 14px;
line-height: 20px;
color: #FFFFFF;
letter-spacing: 1.4px;
}
&:nth-child(2){
line-height: 24px;
font-size: 18px;
color: #FFFFFF;
}
}
}
}
.alert-content-detail{
// height: 100%;
height: calc(100% - 100px);
/* overflow-y: auto; */
overflow: hidden;
width: 98%;
margin-top: -26px;
}
.alert-details-pdf{
width: 102%;
height: 100%;
overflow-y: auto;
padding: 20px 60px;
.alert-details-button{
display: flex;
flex-direction: row;
justify-content: flex-end;
margin-bottom: 10px;
p{
color:#fff;
line-height: 35px;
font-size: 16px;
margin-right: 20px;
}
a{
line-height: 35px;
font-size: 16px;
margin-right: 20px;
}
}
}
.cameraWrap{
width: 100%;
height: 102%;
box-sizing: border-box;
padding: 12px 4px 0 14px;
}
}
用法
import Toptips from "./Toptips"
Toptips.open({
alertTitle: '批示详情',
alertTip: that.htms(val),
closeAlert: function () {
console.log("关闭了...");
}
});
htms = val => {
return (<div className="alert-detais-list">
<div className="alert-detais-list-C">
<p>批示内容:</p>
<p>{val.fdTitle}</p>
</div>
<div className="alert-detais-list-C">
<p>批示详述:</p>
<p>{val.fdTitle}</p>
</div>
<div className="alert-detais-list-C">
<p>措施及结果:</p>
<p>{val.fdContent}</p>
</div>
<div className="alert-detais-list-C">
<p>进度详情:</p>
<p></p>
</div>
</div>)
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
创建一个执行分为两个阶段:创建阶段和执行阶段。创建阶段会发生三件事:绑定this指向,也被称为 This Binding、创建词法环境组件、创建变量环境组件。
这篇文章主要为大家介绍了element plus的样式修改和扩展实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
这篇文章主要介绍了 关于JavaScript 中 if包含逗号表达式,有时会看到JavaScript中if判断里包含英文逗号 “,”,这个是其实是逗号表达式。在if条件里,只有最后一个表达式起判断作用。下面来看看文章的具体介绍吧
js如何去除数组中的empty undefined空项 目录 去除数组中的empty undefined空项 去掉数组中无效的值,比如null,undefined,empty 方法一 方法二 去除数组中的empty undefined空项 arr.filter(d=>d) 注意,0也会被去掉 去掉数组中无效的值,比如null,undefined,empty 方法一 直接使用filter就能搞定了 var arr=[1,2,null,undefined,9]; arr=arr.filter(Boolean); 结果arr=[1,2,9] 方法二 ...
前言过去有很长一段时间,我一直很难理解 reduce() 这个方法的具体用法,平时也很少用到它。事实上,如果你能真正了解它的话,其实在很多地方我们都可以用得上,那么今天我们就来简单聊聊JS中 reduce() 的用法。一、语法arr.reduce(function(prev,cur,index,arr){...}, i
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008