原文地址 小寒的博客

这里的弹窗泛指所有的弹出组件,这些组件不受页面其他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弹窗组件的更多相关文章

  1. 实现一个带有动效的 React 弹窗组件

    我们在写一些 UI 组件时,若不考虑动效,就很容易实现,主要就是有无的切换(类似于 Vue 中的 v-if 属性)或者可见性的切换(类似于 Vue 中的 v-show 属性). 1. 没有动效的弹窗 ...

  2. 封装React AntD的dialog弹窗组件

    前一段时间分享了基于vue和element所封装的弹窗组件(封装Vue Element的dialog弹窗组件),今天就来分享一个基于react和antD所封装的弹窗组件,反正所使用的技术还是那个技术, ...

  3. beeshell —— 开源的 React Native 组件库

    介绍 beeshell 是一个 React Native 应用的基础组件库,基于 0.53.3 版本,提供一整套开箱即用的高质量组件,包含 JavaScript(以下简称 JS)组件和复合组件(包含 ...

  4. 移动web端的react.js组件化方案

     背景: 随着互联网世界的兴起,web前端开发的方式越来越多,出现了很多种场景开发的前端架构体系,也对前端的要求日益增高,早已经不是靠一个JQuery.js来做前端页面的时代了,而今移动端变化最大,近 ...

  5. Griddle, griddle-react 一个REACT 表格组件

    Griddle, griddle-react 一个REACT 表格组件: http://griddlegriddle.github.io/Griddle/index.html

  6. React Native组件之Text

    React Native组件之Text相当于iOS中的UILabel. 其基本属性如下: /** * Sample React Native App * https://github.com/face ...

  7. React Native组件之Switch和Picker和Slide

    React Native组件Switch类似于iOS中的UISwitch:组件Slide类似于iOS中UIslider,组件Picker类似于iOS的UIPickerView.他们的使用方法和相关属性 ...

  8. reactjs入门到实战(七)---- React的组件的生命周期

    React的组件的生命周期有三个状态分别是:挂载(生产组件示例化.准备挂载到页面.挂载到页面).更新(更新值.更新DOM).和卸载(卸载后). >>>其他     getInitia ...

  9. React Native 组件之TextInput

    React Native 组件之TextInput类似于iOS中的UITextView或者UITextField,是作为一个文字输入的组件,下面的TextInput的用法和相关属性. /** * Sa ...

随机推荐

  1. centos 7 ifcnfig提示:bash: ifconfig: command not found的解决方法

    接着上一篇,配置完IP地址之后因为ip addr命令不符合我们的习惯,需要添加ifconfig命令 输入命令 yum -y install net-tools 即可解决

  2. scp 传输下载

    利用scp传输文件 1.从服务器下载文件 scp username@servername:/path/filename /tmp/local_destination 例如scp codinglog@1 ...

  3. 利用IK分词器,自定义分词规则

    IK分词源码下载地址:https://code.google.com/p/ik-analyzer/downloads/list lucene源码下载地址:http://www.eu.apache.or ...

  4. Python3数据分析与挖掘建模实战✍✍✍

    Python3数据分析与挖掘建模实战 Python数据分析简介 Python入门 运行:cmd下"python hello.py" 基本命令: 第三方库 安装 Windows中 p ...

  5. 16-MySQL-Ubuntu-数据表的查询-分组与聚合(五)

    分组(group by)一般与聚合结合使用 (1)查询按性别分组 select gender from students group by gender; (2)查询按性别分组并统计每组的数量sele ...

  6. Codeforces Round #525 D - Ehab and another another xor problem /// 构造

    题目大意: 本题有两个隐藏起来的a b(1<=a,b<=1e30) 每次可 printf("? %d %d\n",c,d); 表示询问 a^c 与 b^d 的相对大小 ...

  7. USACO2005 City Skyline /// oj23401

    题目大意: Input * Line 1: Two space separated integers: N and W * Lines 2..N+1: Two space separated inte ...

  8. HDU—4046 Panda (线段树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4046   题意:给出一个字符串,统计这个字符串任意区间中"wbw"出现的次数. 规定两 ...

  9. exe4j 打包(多个jar打包)

    一,自行下载exe4j 注册码: 用户名和公司名可随便填A-XVK258563F-1p4lv7mg7savA-XVK209982F-1y0i3h4ywx2h1A-XVK267351F-dpurrhny ...

  10. Android开发 retrofit下载与上传

    前言 此博客只讲解retrofit下载与上传的使用,其实与其说是retrofit的下载与上传还不如说,依然是Okhttp的下载与上传.如果你需要了解retrofit入门请查看这篇博客(此博客不在详细讲 ...