原文地址 小寒的博客

这里的弹窗泛指所有的弹出组件,这些组件不受页面其他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. SpringMVC(AbstractController,拦截器,注解)

    1.Controller接口及其实现类 Controller是控制器/处理器接口,只有一个方法handleRequest,用于进行请求的功能处理(功能处理方法),处理完请求后返回ModelAndVie ...

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

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

  3. ES6 学习 -- Promise对象

    1.promise含义:可以将promise对象看成是一个容器,它保存着未来才会结束的某个事件(一般是异步操作事件)的结果,各 种异步操作都可以用promise对象来处理promise的特点:(1)p ...

  4. 2019-9-11-在-P2P-文件分享应用以文件或文件段为单位的优缺

    title author date CreateTime categories 在 P2P 文件分享应用以文件或文件段为单位的优缺 lindexi 2019-09-11 10:23:27 +0800 ...

  5. VSCode 常用setiings.json设置

    { , , "editor.multiCursorModifier": "ctrlCmd", "editor.snippetSuggestions&q ...

  6. 靠谱助手 BlueStacks

    靠谱助手  BlueStacks 安卓虚拟机

  7. FCC知识点总结

    1.DOMContentLoaded事件 2.延迟脚本 defer 3.异步脚本async 4.[找最长单词]—— 找出句子中最长的单词,并返回它的长度. 5.数组slice().splice() s ...

  8. Font Awesome (Mark)

    Font Awesome为您提供可缩放的矢量图标,您可以使用CSS所提供的所有特性对它们进行更改,包括:大小.颜色.阴影或者其它任何支持的效果. 一个字库,675个图标 仅一个Font Awesome ...

  9. Java学习 时间类 Period类与Duration类 / LocalDate类与Instant类 用法详解

    前言 java 8 中引入的两个与日期相关的新类:Period 和 Duration.两个类看表示时间量或两个日期之间的差,两者之间的差异为:Period基于日期值,而Duration基于时间值.他们 ...

  10. Android 开发 框架系列 OkHttp文件下载功能实现(含断点续传)

    前言 此篇博客只是下载功能的记录demo,如果你还不太了解okhttp可以参考我的另一篇博客https://www.cnblogs.com/guanxinjing/p/9708575.html 代码部 ...