React控制元素顯示隱藏的三種方法小結(jié)
React控制元素顯示隱藏的方法
React控制元素顯示和隱藏的方法目前我知道的有三種方法:
- 第一種是通過state變量來控制是否渲染元素,類似vue中的v-if。
- 第二種是通過style控制display屬性,類似vue 中的v-show。
- 第三種是通過動態(tài)切換className。
方法一
第一種方法是通過此例中showElem變量來控制是否加載元素的,如果showElem為false,內(nèi)容是直接不會渲染的。
class Demo extends React.Component{ ? ? constructor(props){ ? ? ? ? super(props); ? ? ? ? this.state = { ? ? ? ? ? ? showElem:true ? ? ? ? } ? ? } ? ? render(){ ? ? ? ? return ( ? ? ? ? ? ? <div> ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? this.state.showElem?( ? ? ? ? ? ? ? ? ? ? ? ? <div>顯示的元素</div> ? ? ? ? ? ? ? ? ? ? ):null ? ? ? ? ? ? ? ? } ? ? ? ? ? ? </div> ? ? ? ? ) ? ? } }
方法二
這個方法很簡單,就是通過display屬性來控制元素顯示和隱藏。
class Demo extends React.Component{ ? ? constructor(props){ ? ? ? ? super(props); ? ? ? ? this.state = { ? ? ? ? ? ? showElem:'none' ? ? ? ? } ? ? } ? ? render(){ ? ? ? ? return ( ? ? ? ? ? ? <div style={{display:this.state.showElem}}>顯示的元素</div> ? ? ? ? ) ? ? } }
方法三
通過className切換hide來實現(xiàn)元素的顯示和隱藏。
class Demo extends React.Component{ ? ? constructor(props){ ? ? ? ? super(props); ? ? ? ? this.state = { ? ? ? ? ? ? showElem:true ? ? ? ? } ? ? } ? ? render(){ ? ? ? ? return ( ? ? ? ? ? ? <div> ? ? ? ? ? ? ? ? {/* 寫法一 */} ? ? ? ? ? ? ? ? <div className={this.state.showElem?'word-style':'word-style hide'}>顯示的元素</div> ? ? ? ? ? ? ? ? {/* 寫法二 */} ? ? ? ? ? ? ? ? <div className={`${this.state.showElem?'':'hide'} word-style`}>顯示的元素</div> ? ? ? ? ? ? </div> ? ? ? ? ) ? ? } }
要注意的是,這幾種方法也有使用的區(qū)別:
方法一不適合頻繁控制顯示隱藏的情況,因為他會重新渲染元素,比較耗費性能。在這種情況下,第二種或者第三種通過display來控制會更合理。
方法一適合安全性高的頁面,比如用戶信息頁面,根據(jù)不同的用戶級別顯示不一樣的內(nèi)容,這時候如果你用方法一或者方法二的話,用戶如果打開network還是可以看見,因為頁面還是渲染了,只是隱藏了而已。而方法一是直接不渲染用戶信息的DOM元素,保證了安全性。
React切換顯示和隱藏
{radioChange >= 0 && ? ? ? ? ? <div> ? ? ? ? ? ? {radioChange === 0 ? ( ? ? ? ? ? ? ? <div className={style.template} key="1"> ? ? ? ? ? ? ? ? <div className={style.inline}>如果金額超過</div> ? ? ? ? ? ? ? ? <Input className={style.input} label=" " id="free_price" rules={['required']} ? ? ? ? ? ? ? ? ? msg={this.msg} style={{ width: '100px', display: 'inlinbe-block' }} /> ? ? ? ? ? ? ? ? <div className={style.inline}>元,免運費,否則按照公里數(shù)收取,每公里</div> ? ? ? ? ? ? ? ? <Input className={style.input} label=" " id="unit_price" rules={['required']} ? ? ? ? ? ? ? ? ? msg={this.msg} style={{ width: '100px', display: 'inlinbe-block' }} /> ? ? ? ? ? ? ? ? <div className={style.inline}>元,最多不超過</div> ? ? ? ? ? ? ? ? <Input className={style.input} label=" " id="max_price" rules={['required']} ? ? ? ? ? ? ? ? ? msg={this.msg} style={{ width: '100px', display: 'inlinbe-block' }} /> ? ? ? ? ? ? ? ? <div className={style.inline}>元</div> ? ? ? ? ? ? ? </div> ? ? ? ? ? ? ) ? ? ? ? ? ? ?: ( ? ? ? ? ? ? ? ?<div className={style.template} key="2"> ? ? ? ? ? ? ? ? ?<div className={style.inline}>如果金額超過</div> ? ? ? ? ? ? ? ? ?<Input className={style.input} label=" " id="free_price" rules={['required']} ? ? ? ? ? ? ? ? ? ?msg={this.msg} style={{ width: '100px', display: 'inlinbe-block' }} /> ? ? ? ? ? ? ? ? ?<div className={style.inline}>元,免運費,否則一口價</div> ? ? ? ? ? ? ? ? ?<Input className={style.input} label=" " id="price" rules={['required']} ? ? ? ? ? ? ? ? ? ?msg={this.msg} style={{ width: '100px', display: 'inlinbe-block' }} /> ? ? ? ? ? ? ? ? ?<div className={style.inline}>元</div> ? ? ? ? ? ? ? ?</div>) ? ? ? ? ? ? } ? ? ? ? ? </div>
如上面代碼顯示,如果通過一個數(shù)值控制,顯示和隱藏切換的話,必須加入一個key值,否則在切換的時候活報錯,應該是在頁面渲染的時候會重復利用這個元素,如果加入keys,渲染的時候,不會產(chǎn)生復用
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
ES6 class類鏈式繼承,實例化及react super(props)原理詳解
這篇文章主要介紹了ES6 class類鏈式繼承,實例化及react super(props)原理,結(jié)合實例形式詳細分析了ES6 中class類鏈式繼承,實例化及react super(props)原理相關概念、原理、定義與使用技巧,需要的朋友可以參考下2020-02-02React事件監(jiān)聽和State狀態(tài)修改方式
這篇文章主要介紹了React事件監(jiān)聽和State狀態(tài)修改方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08React在弱網(wǎng)環(huán)境下限制按鈕多次點擊,防止重復提交問題
這篇文章主要介紹了React在弱網(wǎng)環(huán)境下限制按鈕多次點擊,防止重復提交問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06