網站首頁 編程語言 正文
本文實例為大家分享了React封裝彈出框組件的方法,供大家參考,具體內容如下
效果圖
文件目錄
alertList.tsx 用于容納彈出框的容器
import React from "react"; export const HAlertList = () => { ? ? return ( ? ? ? ? <div ? ? ? ? ? ? id="alert-list" ? ? ? ? ? ? style={{ ? ? ? ? ? ? ? ? position:'fixed', ? ? ? ? ? ? ? ? top: '6%', ? ? ? ? ? ? ? ? left: '50%', ? ? ? ? ? ? ? ? transform: `translate(-50%)` ? ? ? ? ? ? }} ? ? ? ? ></div> ? ? ) }
將該組件置于項目根目錄下的index.tsx
export const root = ReactDOM.createRoot( ? document.getElementById('root') as HTMLElement ); root.render( ? // <React.StrictMode> ? <> ? ? <Provider store={store}> ? ? ? <BrowserRouter> ? ? ? ? <App /> ? ? ? ? <HAlertList/> ? ? ? </BrowserRouter> ? ? </Provider> ? </> ? // </React.StrictMode> );
index.tsx 用于創建單個alert
規定傳入的參數及類型
export interface HAlertProps { ? ? status:'success' | 'error', ? ? text:string }
傳入一個狀態success
或者error
,用于區別樣式
export const HAlert = (props:HAlertProps) => { ? ? return ( ? ? ? ? <AlertContainer status={props.status}> ? ? ? ? ? ? {props.text} ? ? ? ? </AlertContainer> ? ? ) } const AlertContainer = styled.div<{ ? ? status:string }>` ? ? width: 65vw; ? ? height: 30px; ? ? background-color: ${props => props.status === 'success' ? '#a8dda8' : '#ff4b4b'}; ? ? text-align: center; ? ? margin-bottom: 10px; `
此處使用emotion
(css-in-js)的技術,即使用js編寫css樣式
當HTML文檔中識別到AlertContainer
標簽時,會轉變為具有對應樣式的div
標簽
use.tsx 函數式調用alert組件
import React, { useState } from 'react' import ReactDOM from 'react-dom/client' import { HAlertProps, HAlert } from './index' export class AlertList { ? ? static list: HAlertProps[] = [] ? ? static el: ReactDOM.Root | null = null ? ? static showAlert = (props: HAlertProps) => { ? ? ? ? let container: ReactDOM.Root ? ? ? ? if (AlertList.el) { ? ? ? ? ? ? container = AlertList.el ? ? ? ? } else { ? ? ? ? ? ? AlertList.el = container = ReactDOM.createRoot( ? ? ? ? ? ? ? ? document.getElementById('alert-list') as HTMLElement ? ? ? ? ? ? ) ? ? ? ? } ? ? ? ? AlertList.list.push(props) ? ? ? ? container.render( ? ? ? ? ? ? <> ? ? ? ? ? ? ? ? {AlertList.list.map((value: HAlertProps, index: number) => { ? ? ? ? ? ? ? ? ? ? return <HAlert {...value} key={index} /> ? ? ? ? ? ? ? ? })} ? ? ? ? ? ? </> ? ? ? ? ) ? ? ? ? setTimeout(() => { ? ? ? ? ? ? AlertList.list.shift() ? ? ? ? ? ? container.render( ? ? ? ? ? ? ? ? <> ? ? ? ? ? ? ? ? ? ? {AlertList.list.map((value: HAlertProps, index: number) => { ? ? ? ? ? ? ? ? ? ? ? ? return <HAlert {...value} key={index} /> ? ? ? ? ? ? ? ? ? ? })} ? ? ? ? ? ? ? ? </> ? ? ? ? ? ? ) ? ? ? ? }, 2000) ? ? } }
使用類編寫對用的函數,是因為類是存儲數據比較好的辦法,AlertList .list
存儲著彈出框容器中所有彈出框的信息,AlertList.el為彈出框容器的節點showAlert
的邏輯:
1.查看AlertList.el
是否有值,如果沒有則創建創建節點
2.將該HAlert
組件的信息存入AlertList .list
中
3.渲染彈出框列表
4.開啟定時器(此處寫的不是特別好),每隔2s取消一個HAlert
原文鏈接:https://blog.csdn.net/qq_56303170/article/details/126004127
相關推薦
- 2022-04-02 Android?WebView如何判斷是否滾動到底部_Android
- 2022-11-26 ASP.NET延遲調用或多次調用第三方Web?API服務_實用技巧
- 2023-01-23 React新擴展函數setState與lazyLoad及hook介紹_React
- 2022-07-04 PyTorch計算損失函數對模型參數的Hessian矩陣示例_python
- 2023-01-18 C#實現圖片輪播功能的示例代碼_C#教程
- 2023-03-27 Python?tkinter中label控件動態改變值問題_python
- 2022-11-05 python?pandas?query的使用方法_python
- 2023-02-02 C++示例講解觀察者設計模式_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支