React創(chuàng)建對話框組件的方法實例
原生的前端體系創(chuàng)建一個對話框可是再簡單不過了。但是如果放到組件化思想下的react體系中,想要制作一個屬于自己的對話框還是有一定的麻煩的。主要遇到的問題有兩個:一是如何在子組件中創(chuàng)建body下的對話框組件,二是如何刪除這個組件。
接下來我們就一步一步解決這兩個問題。
我們先寫好dialog組件:(所有的樣式都不寫了,這里實現(xiàn)一個原型)
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>//這里接收父組件傳遞過來的關(guān)閉對話框的方法 <a className="btns btns-blue">確定</a> </div> </div> ) } }
動態(tài)創(chuàng)建組件到body中,react為我們提供了一個方法:ReactDOM.unstable_renderSubtreeIntoContainer(parent,component,dom),parent一般是this,組件就是對話框組件,dom就是要插入的dom節(jié)點。
根據(jù)這個方法,我們就可以為對話框?qū)懸粋€父組件,用于全屏居中顯示:
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>這是內(nèi)容內(nèi)容內(nèi)容</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; } }
這樣我們就解決了第一個問題,那么接下來我們要怎樣調(diào)用這個組件呢?
下面是調(diào)用對話框的父組件
//啟動對話框,選擇職業(yè),開始考試 class BeginExamComponent extends Component{ constructor(props){ super(props); } //使用函數(shù)在render中動態(tài)創(chuàng)建組件 renderDialog(){ if (this.props.isShow){ console.log("開始創(chuàng)建對話框組件"); return(//將關(guān)閉對話框的方法傳遞下去 <DialogCenter onClickCancle={this.props.onButtonClose.bind(this)}/> ) }else{ return null;//這里實際上就是所謂的刪除組件 } } render(){ return( <div className="begin-exam-area"> <div className="top-tips">點擊按鈕,請確認(rèn)信息后開始考試</div> <div className="button-wrapper"> <button onClick={this.props.onButtonClick.bind(this)}>開始考試</button>//啟動對話框的函數(shù) <button>模擬考試</button> </div> {this.renderDialog()} </div> ) } }
這里我們可以看到,我們使用了一個renderDialog函數(shù)在render中動態(tài)創(chuàng)建對話框組件,之所以可以這樣直接寫進去,主要是我們之前的DialogCenter組件實現(xiàn)了ReactDOM.unstable_renderSubtreeIntoContainer方法,因此這個組件將會直接在body直接子節(jié)點中渲染。
export class Home extends Component{ constructor(){ super(); this.state={ showDialog:false } } showDialog(){ console.log("調(diào)用對話框"); 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控制組件的創(chuàng)建與否,就目前來看是創(chuàng)建對話框組件的核心。從這里可以實現(xiàn)很多有意思的東西,就看怎么去琢磨了。
總結(jié)
到此這篇關(guān)于React創(chuàng)建對話框組件的文章就介紹到這了,更多相關(guān)React創(chuàng)建對話框組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React Fiber中面試官最關(guān)心的技術(shù)話題
這篇文章主要為大家介紹了React Fiber中面試官最關(guān)心的技術(shù)話題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06關(guān)于React動態(tài)加載路由處理的相關(guān)問題
這篇文章主要介紹了關(guān)于React動態(tài)加載路由處理的相關(guān)問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01在React中如何優(yōu)雅的處理事件響應(yīng)詳解
這篇文章主要給大家介紹了關(guān)于在React中如何優(yōu)雅處理事件響應(yīng)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-07-07詳解對于React結(jié)合Antd的Form組件實現(xiàn)登錄功能
這篇文章主要介紹了詳解對于React結(jié)合Antd的Form組件實現(xiàn)登錄功能,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04react項目中redux的調(diào)試工具不起作用的解決
這篇文章主要介紹了react項目中redux的調(diào)試工具不起作用的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01