網站首頁 編程語言 正文
React Hook實現對話框組件,供大家參考,具體內容如下
準備
- 思路:對話框組件是有需要的時候希望它能夠彈出來,不需要的時候在頁面上是沒有任何顯示的,這就意味著需要一個狀態,在父組件點擊按鈕或其他的時候能夠更改+ 它并顯示,同時子組件(對話框組件)點擊關閉的時候也需要更改這個狀態。
? ? ? 進階:為對話框組件背景添加蒙皮,當對話框內容需要滾動時限制瀏覽器頁面的滾動。
- React Hook 主要用到了 useState,useContext,useContext 主要用于將父組件的 setStatus 傳遞給子組件以至于它可以更新狀態。
附上 Dialog 和 DialogMain組件代碼,其中DialogMain 為父組件,使用時只需將其掛載至 App.js。
代碼
DialogMain 父組件代碼
import React from "react";
import Dialog from "./dialog";
//初始化 Context
export const StatusContext = React.createContext();
const DialogTest = () => {
? ? //設置狀態控制對話框的打開與關閉
? ? const [status, setStatus] = React.useState(false);
? ? //條件渲染,符合條件返回 Dialog 組件否則返回 null
? ? function GetDialog() {
? ? ? ? if (status) return <Dialog />;
? ? ? ? else return null;
? ? }
? ? //打開函數
? ? function openDialog() {
? ? ? ? //打開時禁止瀏覽器外面的滾動,注釋掉這行鼠標在對話框外面時可以滾動瀏覽器界面
? ? ? ? document.body.style.overflow = "hidden";
? ? ? ? setStatus(true);
? ? }
? ? return (
? ? ? ? <div>
? ? ? ? ? ? {/* 按鈕綁定打開函數 */}
? ? ? ? ? ? <button onClick={openDialog}>打開對話框</button>
? ? ? ? ? ? {/* 使用 Context 的 Provider 暴露 setStatus 方法 */}
? ? ? ? ? ? <StatusContext.Provider value={{ setStatus }}>
? ? ? ? ? ? ? ? <GetDialog />
? ? ? ? ? ? </StatusContext.Provider>
? ? ? ? </div>
? ? );
};
export default DialogTest;
Dialog子組件代碼
import React from "react";
import { StatusContext } from "./dialogMain";
const Dialog = (props) => {
? ? //利用解構賦值將 setState 從 useContext分離出來
? ? const { setStatus } = React.useContext(StatusContext);
? ? //關閉時更改 Status 狀態為 false
? ? function uninstallDialog() {
? ? ? ? setStatus(false);
? ? ? ? //關閉時重新將瀏覽器界面設置為滾動或其他
? ? ? ? document.body.style.overflow = "scroll";
? ? }
? ? // 點擊蒙層本身時關閉對話框,點擊對話框的內容時不關閉
? ? function handleClick(event) {
? ? ? ? if (event.target === event.currentTarget) {
? ? ? ? ? ? //調用關閉的方法
? ? ? ? ? ? uninstallDialog();
? ? ? ? }
? ? }
? ? return (
? ? ? ? // 為整個組件添加css 樣式并讓其置于頂層,同時對話框意外的地方添加模糊效果
? ? ? ? <div
? ? ? ? ? ? style={{
? ? ? ? ? ? ? ? position: " fixed",
? ? ? ? ? ? ? ? top: 0,
? ? ? ? ? ? ? ? left: 0,
? ? ? ? ? ? ? ? right: 0,
? ? ? ? ? ? ? ? bottom: 0,
? ? ? ? ? ? ? ? background: "rgba(0, 0, 0, 0.3)",
? ? ? ? ? ? ? ? zIndex: " 99",
? ? ? ? ? ? }}
? ? ? ? ? ? onClick={handleClick}
? ? ? ? >
? ? ? ? ? ? {/* 為對話框添加 css 樣式,absolute定位時相對于容器的位置 ,同時將 overflow 設置為自動*/}
? ? ? ? ? ? <div
? ? ? ? ? ? ? ? style={{
? ? ? ? ? ? ? ? ? ? position: "absolute",
? ? ? ? ? ? ? ? ? ? left: "50%",
? ? ? ? ? ? ? ? ? ? top: "50%",
? ? ? ? ? ? ? ? ? ? overflow: "auto",
? ? ? ? ? ? ? ? ? ? transform: "translate(-50%, -50%)",
? ? ? ? ? ? ? ? ? ? padding: "30px 30px",
? ? ? ? ? ? ? ? ? ? width: " 200px",
? ? ? ? ? ? ? ? ? ? height: " 200px",
? ? ? ? ? ? ? ? ? ? border: "2px solid yellow",
? ? ? ? ? ? ? ? ? ? borderRadius: "8px",
? ? ? ? ? ? ? ? ? ? background: "yellow",
? ? ? ? ? ? ? ? }}
? ? ? ? ? ? >
? ? ? ? ? ? ? ? {/* 為關閉按鈕綁定關閉方法 */}
? ? ? ? ? ? ? ? <button onClick={uninstallDialog}>關閉</button>
? ? ? ? ? ? ? ? 對話框對話框對話框對話框對話框對話框對話框對話框對話框對話框對話框對話框對話框對話框對話框對話框對話框對話框對話框對話框對話框對話框對話框
? ? ? ? ? ? ? ? 對話框對話框對話框對話框對話框對話框對話框對話框對話框對話框對話框對話框對話框
? ? ? ? ? ? ? ? 對話框 對話框 對話框 對話框 對話框 對話框 對話框 對話框 對話框
? ? ? ? ? ? </div>
? ? ? ? </div>
? ? );
};
export default Dialog;
App.js代碼
import "./App.css";
import DialogMain from "./component/dialogTest/dialogMain";
function App() {
? ? return (
? ? ? ? <div className="App">
? ? ? ? ? ? <div style={{ height: "2000px" }}>
? ? ? ? ? ? ? ? <DialogMain />
? ? ? ? ? ? </div>
? ? ? ? </div>
? ? );
}
export default App;
運行界面
原文鏈接:https://blog.csdn.net/weixin_45703889/article/details/125726058
相關推薦
- 2022-09-26 python中創建一個包并引用使用的操作方法_python
- 2022-06-26 C#實現數組元素的數據類型轉換方法詳解_C#教程
- 2022-07-30 Hadoop中hdfs與yarn命令大全
- 2024-01-07 Plugin ‘org.springframework.boot:spring-boot-maven
- 2022-08-22 Nginx配置使用詳解_nginx
- 2022-04-25 Pandas?中的join函數應用實現刪除多余的空行_python
- 2022-11-19 C語言數據結構不掛科指南之隊列詳解_C 語言
- 2022-07-31 Python如何生成隨機數及random隨機數模塊應用_python
- 最近更新
-
- 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同步修改后的遠程分支