React封裝彈出框組件的方法
本文實例為大家分享了React封裝彈出框組件的方法,供大家參考,具體內(nèi)容如下
效果圖



文件目錄

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 用于創(chuàng)建單個alert
規(guī)定傳入的參數(shù)及類型
export interface HAlertProps {
? ? status:'success' | 'error',
? ? text:string
}傳入一個狀態(tài)success或者error,用于區(qū)別樣式
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)的技術(shù),即使用js編寫css樣式
當(dāng)HTML文檔中識別到AlertContainer標(biāo)簽時,會轉(zhuǎn)變?yōu)榫哂袑?yīng)樣式的div標(biāo)簽
use.tsx 函數(shù)式調(diào)用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)
? ? }
}使用類編寫對用的函數(shù),是因為類是存儲數(shù)據(jù)比較好的辦法,AlertList .list存儲著彈出框容器中所有彈出框的信息,AlertList.el為彈出框容器的節(jié)點showAlert的邏輯:
1.查看AlertList.el是否有值,如果沒有則創(chuàng)建創(chuàng)建節(jié)點
2.將該HAlert組件的信息存入AlertList .list中
3.渲染彈出框列表
4.開啟定時器(此處寫的不是特別好),每隔2s取消一個HAlert
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
React 中常用的幾種路由跳轉(zhuǎn)方式小結(jié)
基本路由跳轉(zhuǎn)是最常見的一種方式,下面介紹React 中常用的幾種路由跳轉(zhuǎn)方式,感興趣的朋友一起看看吧2023-12-12
React在Dva項目中創(chuàng)建并引用頁面局部組件的方式
這篇文章主要介紹了React在Dva項目中創(chuàng)建并引用頁面局部組件的方式,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07
React?中hooks之?React.memo?和?useMemo用法示例總結(jié)
React.memo是一個高階組件,用于優(yōu)化函數(shù)組件的性能,通過記憶組件渲染結(jié)果來避免不必要的重新渲染,合理使用React.memo和useMemo可以顯著提升React應(yīng)用的性能,本文介紹React?中hooks之?React.memo?和?useMemo用法總結(jié),感興趣的朋友一起看看吧2025-01-01
詳解三種方式在React中解決綁定this的作用域問題并傳參
這篇文章主要介紹了詳解三種方式在React中解決綁定this的作用域問題并傳參,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
redux功能強大的Middleware中間件使用學(xué)習(xí)
這篇文章主要為大家介紹了redux功能強大的Middleware中間件使用學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09

