原文地址 小寒的博客

这里的弹窗泛指所有的弹出组件,这些组件不受页面其他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. <爬虫实战>豆瓣电影TOP250(三种解析方法)

    1.豆瓣电影排行.py # 目标:爬取豆瓣电影排行榜TOP250的电影信息 # 信息包括:电影名字,上映时间,主演,评分,导演,一句话评价 # 解析用学过的几种方法都实验一下①正则表达式.②Beaut ...

  2. jdbc出现中文乱码的解决办法

  3. Jmeter----参数化之csv文件

    用户编码:一般不用填写,如果有中文的时候,需要填写,填写utf-8就可以 变量名词:是指的把数据保存在什么变量里面,其他的默认就好了 Http请求和用户参数设置的一样

  4. JVM基本知识总结

    大概两三个月之前阅读了<深入理解Java虚拟机>(周志明著),也为了加深印象,这里简单的做下总结,想完整点了解JVM知识的也可以阅读本书,书写的不错,相当通俗易懂. 第一部分 内存管理机制 ...

  5. c# 使用Expression 生成sql

    使用Expression 生成sql  update语句的时候遇到了个问题 ,Expression<Action<T>>  la   这个委托里面老获取不到 引用类型的值,甚至 ...

  6. 当对象转换成JSON的时候处理时间格式

    /// <summary> /// 格式化日期 /// </summary> /// <param name="foramt">格式化规则< ...

  7. centos lamp笔记

    cron and crontab are missing in docker image of ubuntu 16.04 在鏡像中沒有 cron 命令 How to install php-redis ...

  8. iptbales无法正常重启

    新主机iptables无法启动关闭和重启 一般是由于没有配文件导致 解决办法 直接touch /etc/sysconfig/iptables 然后就可以正常启动. 备注:一般存在于centos6系列中

  9. luoguP1029 最大公约数和最小公倍数问题 [gcd][数论]

    题目描述 输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P,Q的个数 条件: 1.P,Q是正整数 2.要求P,Q以x0为 ...

  10. Myeclipse从外部导入项目时,jsp和html页面中所有的onclick="return xx()"位置均出现cannot return from outside function() or method()错误