import React, { useState, useRef, useEffect } from 'react'
import PropTypes from 'prop-types'
import _ from 'lodash'
import uuidv1 from 'uuid/v1' import './index.less' let currentTipComponents = []
let setCurrentTipComponents function GlobalTipContainer(props) {
const { style } = props
const wrapperStyle = _.assign({}, style) const [tipComponents, setTipComponents] = useState([])
currentTipComponents = tipComponents
setCurrentTipComponents = setTipComponents const [displayedTipComponents, setDisplayedTipComponents] = useState([])
const [mouseHover, setMouseHover] = useState(false) const wrapElemRef = useRef(null)
useEffect(() => {
if (mouseHover === false) {
wrapElemRef.current.scrollTop = 10e10
}
}) useEffect(() => {
setDisplayedTipComponents(tipComponents.slice(0, 2))
}, [tipComponents]) return (
<div
className="global-tip-container-component-wrap"
ref={wrapElemRef}
onMouseEnter={() => {
setMouseHover(true)
}}
onMouseLeave={() => {
setMouseHover(false)
}}
style={wrapperStyle}
>
{displayedTipComponents}
</div>
)
} GlobalTipContainer.addTip = (tipComponent) => {
const key = uuidv1()
currentTipComponents = currentTipComponents.slice(0)
currentTipComponents.push(React.cloneElement(tipComponent, {
key,
onClose: _.flow(tipComponent.props.onClose || _.noop, () => { GlobalTipContainer.removeTip(key) }),
}))
setCurrentTipComponents(currentTipComponents)
return key
} GlobalTipContainer.removeTip = (key) => {
const index = _.findIndex(currentTipComponents, ['key', key])
if (index !== -1) {
currentTipComponents.splice(index, 1)
setCurrentTipComponents(currentTipComponents.slice(0))
}
} GlobalTipContainer.propTypes = {
style: PropTypes.object,
} GlobalTipContainer.defaultProps = {
style: {},
} export default GlobalTipContainer

react实现提示消息容器,可以动态添加,删除内部子提示消息的更多相关文章

  1. Android 在布局容器中动态添加控件

    这里,通过一个小demo,就可以掌握在布局容器中动态添加控件,以动态添加Button控件为例,添加其他控件同样道理. 1.addView 添加控件到布局容器 2.removeView 在布局容器中删掉 ...

  2. 编辑 Ext 表格(一)——— 动态添加删除行列

    一.动态增删行 在 ext 表格中,动态添加行主要和表格绑定的 store 有关, 通过对 store 数据集进行添加或删除,就能实现表格行的动态添加删除.   (1) 动态添加表格的行  gridS ...

  3. 用Javascript动态添加删除HTML元素实例 (转载)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. js实现网页收藏功能,动态添加删除网址

    <html> <head> <title> 动态添加删除网址 </title> <meta charset="utf-8"&g ...

  5. jquery动态添加删除div--事件绑定,对象克隆

    我想做一个可以动态添加删除div的功能.中间遇到一个问题,最后在manong123.com开发文摘 版主的热心帮助下解答了(答案在最后) 使用到的jquery方法和思想就是:事件的绑定和销毁(unbi ...

  6. jQuery动态添加删除CSS样式

    jQuery框架提供了两个CSS样式操作方法,一个是追加样式addClass,一个是移除样式removeClass,下面通过一个小例子讲解用法. jQuery动态追加移除CSS样式 <!DOCT ...

  7. JS动态添加删除html

    本功能要求是页面传一个List 集合给后台而且页面可以动态添加删除html代码需求如下: 下面是jsp页面代码 <%@ page language="java" pageEn ...

  8. zookeeper动态添加/删除集群中实例(zookeeper 3.6)

    一,用来作为demo操作的zookeeper集群中的实例: 机器名:zk1 server.1=172.18.1.1:2888:3888 机器名:zk2 server.2=172.18.1.2:2888 ...

  9. C#控制IIS动态添加删除网站

    我的目的是在Winform程序里面,可以直接启动一个HTTP服务端,给下游客户连接使用. 查找相关技术,有两种方法: 1.使用C#动态添加网站应用到IIS中,借用IIS的管理能力来提供HTTP接口.本 ...

随机推荐

  1. Authentication failure. Retrying - 彻底解决vagrant up时警告

    碰到的问题 使用vagrant启动虚拟机时,出现如下警告: vagrant up default: Warning: Authentication failure. Retrying... 原因分析 ...

  2. 文献阅读 | Epigenetics in ENS development and Hirschsprung disease

    系列篇: Epigenetics in ENS development and Hirschsprung disease - Review Epigenetic Mechanisms in Hirsc ...

  3. idea断点调试学习随笔

    1,rerun XXX,这个就是直接重新跑某个程序.2,这个相当于eclipse里面的f8,直接跑完,到下一个断点停下,没有就直接跑完程序.3,停止项目或者程序.要是自己的main呢,点一下就停下了, ...

  4. List和Array相互转换

    List<String> list = new ArrayList<String>(); list.add("1"); list.add("2&q ...

  5. composer Changed current directory to没反应

    根据官方手册执行composer global require "laravel/installer" 显示Changed current directory to C:/User ...

  6. flutter 不规则底部工具栏实现

    import 'package:flutter/material.dart'; import 'each_view.dart'; class BottomAppBarDemo extends Stat ...

  7. Linux命令 – ln 软连接与硬链接区别介绍

    ln命令可以生成软链接和硬链接,也可叫做符号链接和实体链接. 有兴趣深入理解的可以查阅相关文档,一般的读者只需记住以下几点即可: 不管是软链接还是硬链接都不会额外增加磁盘空间(虽然实际情况可能会多占用 ...

  8. sql 中 '' 与 null 的区别

    SELECT * from t_company where content = '' SELECT * from t_company where content is null update t_co ...

  9. Flutter利用GridView实现网格的商品布局

    GridView.count 生成的是静态网格 效果: 代码: import 'package:flutter/material.dart'; void main() { runApp(MyApp() ...

  10. TCP和TLS/SSL会话细节

    TCP数据段格式说明TCP建立连接和断开连接细节Https如何保证通信安全一次Https网络请求通信细节网络数据包分析工具wireshark的使用问题:SYN.ACK.FIN具体含义是什么?TCP建立 ...