日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

React控制元素顯示隱藏的三種方法小結_React

作者:叉叉醬 ? 更新時間: 2022-12-29 編程語言

React控制元素顯示隱藏的方法

React控制元素顯示和隱藏的方法目前我知道的有三種方法:

  • 第一種是通過state變量來控制是否渲染元素,類似vue中的v-if。
  • 第二種是通過style控制display屬性,類似vue 中的v-show。
  • 第三種是通過動態(tài)切換className。

方法一

第一種方法是通過此例中showElem變量來控制是否加載元素的,如果showElem為false,內容是直接不會渲染的。

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來實現元素的顯示和隱藏。

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來控制會更合理。

方法一適合安全性高的頁面,比如用戶信息頁面,根據不同的用戶級別顯示不一樣的內容,這時候如果你用方法一或者方法二的話,用戶如果打開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}>元,免運費,否則按照公里數收取,每公里</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>

如上面代碼顯示,如果通過一個數值控制,顯示和隱藏切換的話,必須加入一個key值,否則在切換的時候活報錯,應該是在頁面渲染的時候會重復利用這個元素,如果加入keys,渲染的時候,不會產生復用

總結

原文鏈接:https://blog.csdn.net/weixin_39782183/article/details/104751527

欄目分類
最近更新