React弹窗组件
原文地址 小寒的博客
这里的弹窗泛指所有的弹出组件,这些组件不受页面其他UI布局影响,处于DOM结构的顶层,绝对定位在body元素下。
这个特殊性也给它的开发提出了特殊的要求。
react新版本中的createPortal Api可以很方便的制造一个组件到制定的dom里。
在componentDidMount中进行ReactDOM.render方法是一个很巧妙的技巧。
话不多说,开始贴代码
1. 在componentDidMount去渲染dom
class Modal extends React.Component {
el = null
componentDidMount() {
this.el = createElementToModalRoot()
renderModal(this.el, this.props)
}
componentDidUpdate() {
renderModal(this.el, this.props)
if (!this.props.visible) {
unRenderModal(this.el)
}
}
componentWillUnMount() {
unRenderModal(this.el)
}
render() {
return null
}
}
上边的代码就是弹窗的核心思想,首先创建element在root元素下,然后渲染dom,在unmount或者visible属性为false的时候,卸载弹窗
而更新属性也是通过重新render去渲染的,由于react伟大的diff算法,我们即使ReactDOM.render重新渲染也不会导致页面刷新,而只是属性的变化引起的页面变动
2. createElementToModalRoot方法
function createElementToModalRoot() {
let modalRoot
modalRoot = document.getElementById('modal-root')
if (!modalRoot) {
modalRoot = document.createElement('div')
modalRoot.setAttribute('id', 'modal-root')
const modalMask = document.createElement('div')
modalMask.setAttribute('id', 'modal-mask')
document.body.appendChild(modalRoot)
modalRoot.appendChild(modalMask)
}
const el = document.createElement('div')
modalRoot.appendChild(el);
return el
}
用dom方法创建元素,因为此时componentDidMount所以我们可以肆无忌惮的进行dom操作了,执行这个方法之后我们会创建#modal-root #modal-mask 以及 待会render的dom元素
3. renderModal方法
const renderModal = (el, props) => {
const modalRoot = document.getElementById('modal-root')
const modalMask = document.getElementById('modal-mask')
modalMask.style.display = 'block'
modalRoot.style.display = 'block'
ReactDOM.render(<ModalInner {...props} />, el)
}
上面添的代码我们用ModalInner组件创建了一个去渲染了添加在#modal-root下面的dom,每次更新组件,也是通过他再次渲染
4. ModalInner组件
class ModalInner extends React.Component {
render() {
const { children, title, visible, onCancel, onOk } = this.props
return (
<div className={classnames('modal', visible ? 'modal-animate-in' : 'modal-animate-out')}>
<div className="modal-head">
<div className="modal-title">{title}</div>
<div className="modal-cancel-btn" onClick={onCancel}>+</div>
</div>
<div className="modal-content">
{children}
</div>
<div className="modal-footer">
<button className="do-btn" onClick={onCancel}>取消</button>
<button className="do-btn do-btn-primary" onClick={onOk}>确定</button>
</div>
</div>
)
}
}
这个组件,我们设置了最常用的一些属性,包括title children visible 和 onCancel onOk
5. unRenderModal方法
最后我们就剩下卸载方法了
const unRenderModal = (el) => {
const modalRoot = document.getElementById('modal-root')
const modalMask = document.getElementById('modal-mask')
modalMask.style.display = 'none'
modalRoot.style.display = 'none'
modalRoot.removeChild(el);
}
6. 添加动画上边的ModalInner组件里可以看到他会根据visible对dom添加不同的animate从而产生动画
但是如果unRenderModal方法会直接移除dom,所以不会产生移除动画
所以我们把上边的componentDidMount修改一下
componentDidUpdate() {
renderModal(this.el, this.props)
if (!this.props.visible) {
setTimeout(() => unRenderModal(this.el), 500)
}
}
7. Modal.open方法
Modal.open = option => {
const props = {...option}
const el = createElementToModalRoot()
const close = () => {
option.visible = false
renderModal(el, option)
setTimeout(() => unRenderModal(el), 500)
}
props.visible = true
props.children = option.content
props.onOk = e => {
option.onOk ? option.onOk(e, close) : close()
}
props.onCancel = () => {
option.ononCancel ? option.ononCancel(e, close) : close()
}
renderModal(el, props)
}
还是用的上面的那些api,这是visible属性是我们手动传入组件里的
这样我们就可以通过非api的形式去打开一个弹窗了
以上便是render方法创建弹窗的方式,当然很推荐使用createPortal方法,可以省去手动render和unRender的过程
React弹窗组件的更多相关文章
- 实现一个带有动效的 React 弹窗组件
我们在写一些 UI 组件时,若不考虑动效,就很容易实现,主要就是有无的切换(类似于 Vue 中的 v-if 属性)或者可见性的切换(类似于 Vue 中的 v-show 属性). 1. 没有动效的弹窗 ...
- 封装React AntD的dialog弹窗组件
前一段时间分享了基于vue和element所封装的弹窗组件(封装Vue Element的dialog弹窗组件),今天就来分享一个基于react和antD所封装的弹窗组件,反正所使用的技术还是那个技术, ...
- beeshell —— 开源的 React Native 组件库
介绍 beeshell 是一个 React Native 应用的基础组件库,基于 0.53.3 版本,提供一整套开箱即用的高质量组件,包含 JavaScript(以下简称 JS)组件和复合组件(包含 ...
- 移动web端的react.js组件化方案
背景: 随着互联网世界的兴起,web前端开发的方式越来越多,出现了很多种场景开发的前端架构体系,也对前端的要求日益增高,早已经不是靠一个JQuery.js来做前端页面的时代了,而今移动端变化最大,近 ...
- Griddle, griddle-react 一个REACT 表格组件
Griddle, griddle-react 一个REACT 表格组件: http://griddlegriddle.github.io/Griddle/index.html
- React Native组件之Text
React Native组件之Text相当于iOS中的UILabel. 其基本属性如下: /** * Sample React Native App * https://github.com/face ...
- React Native组件之Switch和Picker和Slide
React Native组件Switch类似于iOS中的UISwitch:组件Slide类似于iOS中UIslider,组件Picker类似于iOS的UIPickerView.他们的使用方法和相关属性 ...
- reactjs入门到实战(七)---- React的组件的生命周期
React的组件的生命周期有三个状态分别是:挂载(生产组件示例化.准备挂载到页面.挂载到页面).更新(更新值.更新DOM).和卸载(卸载后). >>>其他 getInitia ...
- React Native 组件之TextInput
React Native 组件之TextInput类似于iOS中的UITextView或者UITextField,是作为一个文字输入的组件,下面的TextInput的用法和相关属性. /** * Sa ...
随机推荐
- k8s 对接glusterfs存储
service 与 endpoint 是通过namespace相同,name相同,相互关联的 创建endpoint [root@k8s-master glusterfs]# cat glusterfs ...
- svn更新的时候断电,下次在更新出现svn: sqlite: database disk image is malformed
svn更新的时候断电,下次在更新出现svn: sqlite: database disk image is malformed 这种悲催的事情竟然发生了 解决办法:
- 5-MySQL高级-事务-回滚(3)
回滚 为了演示效果,需要打开两个终端窗口,使用同一个数据库,操作同一张表 step1:连接 终端1 select * from goods_cates; step2:增加数据 终端2:开启事务,插入数 ...
- shell对比两个目录中有哪些文件不同
参考:https://www.cnblogs.com/liyuchuan/p/10756066.html 目录结构: /data/dir1 subdir/ subfile /data/dir2 sub ...
- NLP杂点
1.停用词 stop words: 在处理自然语言数据(或文本)之前或之后会自动过滤掉某些字或词. 停用词都是人工输入.或者由一个停用词表导入. 2.jieba是目前最好的 Python 中文分词组件 ...
- redis随记
CONFIG REWRITE 将config文件 将服务器当前所使用的配置记录到 redis.conf 文件中.
- RAKsmart服务器受消费者青睐的原因
随着互联网的快速发展,网站建设变得越来越重要,现在很多做外贸网站的都会选择美国服务器来建设网站,近年来RAKsmart服务器受到广大站长的欢迎,那RAKsmart服务器受消费者青睐的关键是什么呢. 1 ...
- 【JZOJ6378】小w与数字游戏(game)
description analysis 对于\(n\)很大,一眼看出来肯定有两个相等的数减出来是\(0\),答案肯定是\(0\) 其实只要\(n>7\),由于斐波那契数列,肯定能有几个数的和减 ...
- Java基本语法.part02
变量 变量的概念: 内存中的一个存储区域 该区域有自己的名称(变量名)和类型(数据类型) Java中每个变量必须先声明,后使用 该区域的数据可以在同一类型范围内不断变化 使用变量注意: 变量的作用域: ...
- lxhgww的奇思妙想 长链剖分板子
https://vijos.org/d/Bashu_OIers/p/5a79a3e1d3d8a103be7e2b81 求k级祖先,预处理nlogn,查询o1 //#pragma GCC optimiz ...