当前位置: 首页 > Web前端 > HTML

React简易版全局消息方法

时间:2023-03-28 11:08:45 HTML

从“反应”中导入反应,{备忘录};从'react-dom'导入ReactDOM;constMessage=memo((props)=>{//console.log('msg:',props)return({props.msg||'已提交'}

)})lettimer=nullconstmessageTip={show(msg){letel=document.getElementById('#message-wrap');//这一步是必须的,因为执行的时候页面还没有挂载,所以获取不到el节点if(!el){el=document.createElement('div')el.style.position='absolute'el.style.top='0'el.style.width='100%'el.id='message-wrap'document.body.append(el)ReactDOM.render(,埃尔);}if(el){timer=setTimeout(()=>{document.body.removeChild(document.getElementById('message-wrap'))},3000);}if(!el&&timer){clearTimeout(timer)}},}导出默认消息提示

最新推荐
猜你喜欢