最近研究react,发现写一个组件很容易,但是要写一个全局的好像有点麻烦。不能像VUE一样,直接在原型上面扩展,注册全局组件

下面实现一个弹框,只需要引入之后,直接调用方法即可,不需要写入组件

给出我写react全局组件的思路。

创建一个 div加入到body,用这个div当容器,渲染react组件,然后由改变组件的 state来控制弹框的显示隐藏

代码结构如下:

效果图如下:

alert.jsx

 

import React, { Component } from 'react';
import { is, fromJS } from 'immutable';
import ReactDOM from 'react-dom';
import ReactCSSTransitionGroup from 'react-addons-css-transition-group';
import './alert.css'; let defaultState = {
alertStatus:false,
alertTip:"提示",
closeAlert:function(){}
} class Alert extends Component{ state = {
...defaultState
};
// css动画组件设置为目标组件
FirstChild = props => {
const childrenArray = React.Children.toArray(props.children);
return childrenArray[0] || null;
}
// 关闭弹框
confirm = () => {
this.setState({
alertStatus:false
})
this.state.closeAlert();
}
open =(options)=>{
options = options || {};
options.alertStatus = true;
this.setState({
...defaultState,
...options
})
}
close(){
this.state.closeAlert();
this.setState({
...defaultState
})
}
shouldComponentUpdate(nextProps, nextState){
return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState))
} render(){
return (
<ReactCSSTransitionGroup
component={this.FirstChild}
transitionName='hide'
transitionEnterTimeout={300}
transitionLeaveTimeout={300}>
<div className="alert-con" style={this.state.alertStatus? {display:'block'}:{display:'none'}}>
<div className="alert-context">
<div className="alert-content-detail">{this.state.alertTip}</div>
<div className="comfirm" onClick={this.confirm}>确认</div>
</div>
</div>
</ReactCSSTransitionGroup>
);
}
} let div = document.createElement('div');
let props = { };
document.body.appendChild(div); let Box = ReactDOM.render(React.createElement(
Alert,
props
),div); export default Box;

  

alert.css

.alert-con {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.3);
z-index: 11;
}
.alert-context {
background-color: #fff;
border-radius: 16px;
height: 200px;
width: 80%;
margin: 40% auto 0;
}
.alert-context .alert-content-detail {
text-align: center;
color: #333;
font-size: 24px;
height: 150px;
line-height: 150px;
}
.alert-context .comfirm {
border-top: 1PX solid #eee;
box-sizing: border-box;
height: 50px;
line-height: 50px;
text-align: center;
font-size: 20px;
color: #666;
}
.alert-enter {
opacity: 0;
}
.alert-enter.alert-enter-active {
transition: all 300ms;
opacity: 1;
}
.alert-leave {
opacity: 1;
}
.alert-leave.alert-leave-active {
transition: all 300ms;
opacity: 0;
}

  

使用方式:

import React, { Component } from 'react';

import Alert from "../components/alert/alert.jsx";

class Two extends Component {
constructor(props){
super(props);
this.state = {
num:1
};
} open=()=>{
Alert.open({
alertTip:"这是一个测试弹框",
closeAlert:function(){
console.log("关闭了...");
}
});
}
render() {
return (
<div className="Two">
Two
<button onClick={this.open}>
开启宝藏
</button>
<div>{this.state.num}</div>
</div>
);
}
} export default Two;

上面直接引入 alert.jsx之后,调用 open函数即可

这样的好处,解决了弹框的层级问题,使用更加方便快捷

react全局的公共组件-------弹框 (Alert)的更多相关文章

  1. layer 一款口碑极佳的web弹层组件,弹框专用

    研究学习网址: http://layer.layui.com/

  2. 操作JavaScript的Alert弹框

    @Testpublic void testHandleAlert(){ WebElement button =driver.findElement(By.xpath("input" ...

  3. selenium浏览器弹出框alert 操作

    1.简介 在WebDriver中要处理JS生成的alert.confirm以及prompt,需要 switch_to.alert() 来选取(定位)警告弹窗,在对弹窗进行关闭.输入等信息操作. 2.操 ...

  4. selenium webdriver从安装到使用(python语言),显示等待和隐性等待用法,切换窗口或者frame,弹框处理,下拉菜单处理,模拟鼠标键盘操作等

    selenium的用法 selenium2.0主要包含selenium IDE 和selenium webDriver,IDE有点类似QTP和LoadRunner的录制功能,就是firefox浏览器的 ...

  5. 弹框alertView

    // 创建一个弹框UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“标题” message:@“显示的具体内容” delegate:s ...

  6. selemiun 下拉菜单、复选框、弹框定位识别

    一.下拉菜单识别 对下拉框的操作,主要是通过Select 类里面的方法来实现的,所以需要new 一个Select 对象(org.openqa.selenium.support.ui.Select)来进 ...

  7. python上selenium的弹框操作

    selenium之弹框操作 1,分类 弹框类型自见解分为四种: 1,页面弹框 2,警告提示框(alert) 3,确认消息框(confirm) 4,提示消息对话(prompt) 提示:selenium ...

  8. react 全局公共组件-----动态弹窗 (dialog)

    react 的时候,总是会用到弹窗,并且各种各样的,一般来说,组件层级嵌套之后,就会出现 z-index层级覆盖的问题 这个时候,就需要一个公共的弹出层,然后我们将需要展示的组件,放到弹出层里面 下面 ...

  9. vue3系列:vue3.0自定义弹框组件V3Popup|vue3.x手机端弹框组件

    基于Vue3.0开发的轻量级手机端弹框组件V3Popup. 之前有分享一个vue2.x移动端弹框组件,今天给大家带来的是Vue3实现自定义弹框组件. V3Popup 基于vue3.x实现的移动端弹出框 ...

随机推荐

  1. 爬虫----爬虫请求库selenium

    一 介绍 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质是通过驱动浏览器,完全模拟浏览器的操作, ...

  2. python基础①

    一.    Python 命名规范: 1, 变量量由字⺟母, 数字,下划线搭配组合⽽而成 2, 不不可以⽤用数字开头,更更不不能是全数字    3,不能是pythond的关键字, 这些符号和字⺟母已经 ...

  3. [转载]win7x64下的redis安装与使用

    原文应用为:`https://www.cnblogs.com/koal/p/5484916.html` 先引用百度百科的一段话吧,具体可以到百科查看吧. Redis是一个开源的使用ANSI C语言编写 ...

  4. 应用打开其xlspptdoc等

    http://www.libxl.com/documentation.html  xls读写编辑类库libxl https://blog.csdn.net/songbob/article/detail ...

  5. node 下查看安装插件的最新版本号的方法

    例如查看extract-text-webpack-plugin的最新版本号 (不一定时本地安装的插件的版本号) npm view extract-text-webpack-plugin version ...

  6. arcengine右键实现new group layer的功能

    没有找到相关方法,但是有对图层组进行操作的资料. https://gis.stackexchange.com/questions/43620/how-do-i-reach-a-layer-inside ...

  7. 分布式事务之TCC服务设计和实现注意事项

    分布式事务之TCC服务设计和实现注意事项-云栖社区-阿里云 https://yq.aliyun.com/articles/609854 分布式事务之TCC事务丶一个站在Java后端设计之路的男青年个人 ...

  8. [development][c++] C++构造函数调用构造函数

    构造函数调用构造函数是会问题的. 外层函数返回的内存, 与被调用的构造函数返回的内存并不是一个内存. 错误示例代码如下: msg_log(const char *name, const char* t ...

  9. CAAnimationDelegate 代理方法没调用

    CAAnimationDelegate 代理方法没调用 应该在 addAnimation调用之前设置代理

  10. 提取json响应结果值_后置处理器JSON Extractor

    Json响应格式 json串中{}表示对象,[]表示数组 JSON Extractor使用json path表达式匹配,可以一次取多个变量值. $表示响应的根对象. 取子对象或对象的属性用. 取数组里 ...