網站首頁 編程語言 正文
原生的前端體系創建一個對話框可是再簡單不過了。但是如果放到組件化思想下的react體系中,想要制作一個屬于自己的對話框還是有一定的麻煩的。主要遇到的問題有兩個:一是如何在子組件中創建body下的對話框組件,二是如何刪除這個組件。
接下來我們就一步一步解決這兩個問題。
我們先寫好dialog組件:(所有的樣式都不寫了,這里實現一個原型)
class Dialog extends Component{
constructor(props){
super(props);
}
render(){
return(
<div className="dialog">
<div className="title">{this.props.title}</div>
<div className="content">{this.props.children}</div>
<div className="button-area">
<a onClick={this.props.onClickCancle.bind(this)} className="btns">取消</a>//這里接收父組件傳遞過來的關閉對話框的方法
<a className="btns btns-blue">確定</a>
</div>
</div>
)
}
}
動態創建組件到body中,react為我們提供了一個方法:ReactDOM.unstable_renderSubtreeIntoContainer(parent,component,dom),parent一般是this,組件就是對話框組件,dom就是要插入的dom節點。
根據這個方法,我們就可以為對話框寫一個父組件,用于全屏居中顯示:
class DialogCenter extends Component{
constructor(props){
super(props);
}
appendToBody() {
ReactDOM.unstable_renderSubtreeIntoContainer(
this,
<Dialog title="this is a title!" onClickCancle={this.props.onClickCancle.bind(this)}>
<span>這是內容內容內容</span>
</Dialog>,
this.container
)
}
componentDidMount() {
this.container = document.createElement('div');
$(this.container).addClass("global-hide");
document.body.appendChild(this.container);
this.appendToBody()
}
componentDidUpdate() {
this.appendToBody()
}
componentWillUnmount() {
document.body.removeChild(this.container)
}
render(){
return null;
}
}
這樣我們就解決了第一個問題,那么接下來我們要怎樣調用這個組件呢?
下面是調用對話框的父組件
//啟動對話框,選擇職業,開始考試
class BeginExamComponent extends Component{
constructor(props){
super(props);
}
//使用函數在render中動態創建組件
renderDialog(){
if (this.props.isShow){
console.log("開始創建對話框組件");
return(//將關閉對話框的方法傳遞下去
<DialogCenter onClickCancle={this.props.onButtonClose.bind(this)}/>
)
}else{
return null;//這里實際上就是所謂的刪除組件
}
}
render(){
return(
<div className="begin-exam-area">
<div className="top-tips">點擊按鈕,請確認信息后開始考試</div>
<div className="button-wrapper">
<button onClick={this.props.onButtonClick.bind(this)}>開始考試</button>//啟動對話框的函數
<button>模擬考試</button>
</div>
{this.renderDialog()}
</div>
)
}
}
這里我們可以看到,我們使用了一個renderDialog函數在render中動態創建對話框組件,之所以可以這樣直接寫進去,主要是我們之前的DialogCenter組件實現了ReactDOM.unstable_renderSubtreeIntoContainer方法,因此這個組件將會直接在body直接子節點中渲染。
export class Home extends Component{
constructor(){
super();
this.state={
showDialog:false
}
}
showDialog(){
console.log("調用對話框");
this.setState({
showDialog:true
})
}
closeDialog(){
console.log("卸載對話框");
this.setState({
showDialog:false
})
}
render(){
return(
<div>
<HomeHeader avatarUid={this.props.account}/>
<SearchArea/>
<BeginExamComponent onButtonClick={this.showDialog.bind(this)} onButtonClose={this.closeDialog.bind(this)} isShow={this.state.showDialog}/>
</div>
)
}
}
通過State控制組件的創建與否,就目前來看是創建對話框組件的核心。從這里可以實現很多有意思的東西,就看怎么去琢磨了。
總結
原文鏈接:https://blog.csdn.net/csu_passer/article/details/75647210
相關推薦
- 2022-04-08 Swift使用表格組件實現單列表_Swift
- 2022-04-20 Python基礎筆記之struct和格式化字符_python
- 2022-11-27 Python線性網絡實現分類糖尿病病例_python
- 2022-09-17 python中pandas常用命令詳解_python
- 2022-09-27 Golang利用位運算實現為程序加速_Golang
- 2022-08-28 虛擬機ubuntu通過fdisk命令擴充硬盤容量,分區簡單說明
- 2022-05-21 python實現會員信息管理系統(List)_python
- 2022-12-21 Android多套環境的維護思路詳解_Android
- 最近更新
-
- 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同步修改后的遠程分支