淺談React碰到v-if
最近在重構(gòu)公司老項目,由于本人以前的技術(shù)棧是vue, 換工作后發(fā)現(xiàn)現(xiàn)在公司的技術(shù)棧是react, 所以重構(gòu)的過程是及其痛苦。加之項目又是幾年前的老項目,不敢大改,比葫蘆畫瓢比比皆是。本文就介紹下遇到的一個常用的痛點。歡迎大佬指正。
廢話不多說,直接上一段代碼。
import React from 'react' const App = () => { const record = { toKe: true, // 貝殼首頁 toSecondHand: true, // 二手房 toFang: true, // 新房 } return ( <div style={{width: 600, margin: '50px auto'}}> <ul> <li> <h3>react常規(guī)寫法</h3> { record.toKe ? <a style={{padding: 20}} rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >貝殼首頁</a> : null } { record.toSecondHand ? <a style={{padding: 20}} rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >二手房</a> : null } { record.toFang ? <a style={{padding: 20}} rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >新房</a> : null } </li> </ul> </div> ) } export default App
如上述代碼,我們在項目中會遇到很多這樣的寫法, 細(xì)看一下沒什么問題,可是當(dāng)在重構(gòu)老項目的時候,你會發(fā)現(xiàn)這個代碼結(jié)構(gòu)是多么痛苦,特別是如下的結(jié)構(gòu)。
{ record.toFang && record.toKe && record.toSecondHand && <div> <a style={{padding: 20}} rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >貝殼首頁</a> <a style={{padding: 20}} rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >二手房</a> <a style={{padding: 20}} rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >新房</a> </div> }
雖然代碼邏輯沒問題,但人生就是這樣,有時候出場順序真的很重要。突然就想起vue的v-if了。
沒錯,回歸主題,就是:react中模擬vue的v-if
先上代碼
import React from 'react' import Hidden from './Hidden' const App = () => { const record = { toKe: true, // 貝殼首頁 toSecondHand: true, // 二手房 toFang: true, // 新房 } return ( <div style={{width: 600, margin: '50px auto'}}> <ul> <li> <h3>react模擬實現(xiàn)vue中v-if指令</h3> <Hidden visible={record.toKe} tag='span'> <a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >貝殼首頁</a> </Hidden> <Hidden visible={record.toSecondHand} tag='span' style={{padding: 20}}> <a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >二手房</a> </Hidden> <Hidden visible={record.toFang} tag='p'> <a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >新房</a> </Hidden> </li> </ul> </div> ) } export default App
簡單就是封裝Hidden組件,通過visible去控制是否渲染。
相信聰明的你一定知道怎么去封裝Hidden
筆者剛開始是這么寫的
import React, { Component } from 'react' export default class Hidden extends Component { render() { const { visible, children } = this.props const content = visible ? children : null return ( <div> { content } </div> ) } }
如此簡單,但筆者審查元素的時候發(fā)現(xiàn),每個Hidden下的children莫名被嵌套了一層div
如下
原來的橫著排列的元素,一下子豎著排列了。這可不太好,本來給我套個div,我都可以勉強接收,現(xiàn)在連我布局都給我變了。
怎么辦?筆者突然想到使用vue-router中的router-link時,標(biāo)簽是可以通過tag去替換默認(rèn)標(biāo)簽的。
那我們再給它個tag唄,連帶自定義屬性。
import React, { Component } from 'react' export default class Hidden extends Component { render() { const { visible, children, tag = 'div', ...rest } = this.props const content = visible ? children : null return ( React.createElement(tag, rest, content) ) // return ( // 嘗試用這種方法去實現(xiàn),發(fā)現(xiàn)不符合react的規(guī)則,所以使用最原始的渲染方法 // React.createElement // `<`${tag}`>` + { content } + `</`${tag}`>` // ) } }
問題:筆者的初衷是模擬vue的v-if, 所以對傳入的children并沒做太多處理,不建議做多做封裝。有興趣的同學(xué)可以自己玩。
{ record.toFang && record.toKe && record.toSecondHand && <span> <a style={{padding: 20}} rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >貝殼首頁</a> <a style={{padding: 20}} rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >二手房</a> <a style={{padding: 20}} rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >新房</a> </span> } <Hidden visible={record.toFang && record.toKe && record.toSecondHand} tag='span'> a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >貝殼首頁</a> <a style={{padding: 20}} rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >二手房</a> <a style={{padding: 20}} rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >新房</a> </Hidden>
其實感覺也沒多大用處hhhh
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
React實現(xiàn)組件間通信的幾種方式小結(jié)
在React應(yīng)用中,組件間的通信是一個基礎(chǔ)而關(guān)鍵的概念,理解和掌握不同組件之間的通信方式,可以幫助我們構(gòu)建出更加模塊化、可維護(hù)和可擴展的應(yīng)用程序,React提供了多種組件通信的方法,本文給大家詳細(xì)的介紹了這些方法,需要的朋友可以參考下2024-07-07react-native使用react-navigation進(jìn)行頁面跳轉(zhuǎn)導(dǎo)航的示例
本篇文章主要介紹了react-native使用react-navigation進(jìn)行頁面跳轉(zhuǎn)導(dǎo)航的示例,具有一定的參考價值,有興趣的可以了解一下2017-09-09聊聊ant?design?charts?獲取后端接口數(shù)據(jù)展示問題
今天在做項目的時候遇到幾個讓我很頭疼的問題,一個是通過后端接口成功訪問并又返回數(shù)據(jù),但拿不到數(shù)據(jù)值。其二是直接修改state中的data,console中數(shù)組發(fā)生變化但任然數(shù)據(jù)未顯示,這篇文章主要介紹了ant?design?charts?獲取后端接口數(shù)據(jù)展示,需要的朋友可以參考下2022-05-05使用react+redux實現(xiàn)計數(shù)器功能及遇到問題
使用redux管理數(shù)據(jù),由于Store獨立于組件,使得數(shù)據(jù)管理獨立于組件,解決了組件之間傳遞數(shù)據(jù)困難的問題,非常好用,今天重點給大家介紹使用react+redux實現(xiàn)計數(shù)器功能及遇到問題,感興趣的朋友參考下吧2021-06-06