網(wǎng)站首頁 編程語言 正文
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值,否則在切換的時候活報錯,應(yīng)該是在頁面渲染的時候會重復(fù)利用這個元素,如果加入keys,渲染的時候,不會產(chǎn)生復(fù)用
總結(jié)
原文鏈接:https://blog.csdn.net/weixin_39782183/article/details/104751527
相關(guān)推薦
- 2022-07-04 python如何使用replace做多字符替換_python
- 2022-11-09 React的特征單向數(shù)據(jù)流學(xué)習(xí)_React
- 2022-12-01 SQL?Server主鍵與外鍵設(shè)置以及相關(guān)理解_MsSql
- 2022-05-22 C#單例模式與多線程用法介紹_C#教程
- 2022-09-09 使用PyTorch實現(xiàn)隨機(jī)搜索策略_python
- 2023-03-04 Golang錯誤處理方式異常與error_Golang
- 2022-04-21 C#實現(xiàn)chart控件動態(tài)曲線繪制_C#教程
- 2022-08-29 使用C#中的Flags特性_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支