小編給大家分享一下React中條件渲染的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供城中網(wǎng)站建設、城中做網(wǎng)站、城中網(wǎng)站設計、城中網(wǎng)站制作等企業(yè)網(wǎng)站建設、網(wǎng)頁設計與制作、城中企業(yè)網(wǎng)站模板建站服務,10多年城中做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡服務。
前言
在React中,你可以創(chuàng)建不同的組件各自封裝你需要的東西。之后你可以只渲染其中的一部分,這取決于應用的state(狀態(tài))。
條件渲染
可以根據(jù)state的值進行組件的條件渲染。例如:
function Greeting(props) { const isLoggedIn = props.isLoggedIn; if (isLoggedIn) { return <UserGreeting />; } return <GuestGreeting />; } ReactDOM.render( // Try changing to isLoggedIn={true}: <Greeting isLoggedIn={false} />, document.getElementById('root') );
你還可以用變量去存儲組件,以便進行條件篩選,使得渲染函數(shù)的返回值更加清爽,例如:
class LoginControl extends React.Component { constructor(props) { super(props); this.handleLoginClick = this.handleLoginClick.bind(this); this.handleLogoutClick = this.handleLogoutClick.bind(this); this.state = {isLoggedIn: false}; } handleLoginClick() { this.setState({isLoggedIn: true}); } handleLogoutClick() { this.setState({isLoggedIn: false}); } render() { const isLoggedIn = this.state.isLoggedIn; let button = null; if (isLoggedIn) { button = <LogoutButton onClick={this.handleLogoutClick} />; } else { button = <LoginButton onClick={this.handleLoginClick} />; } return ( <div> <Greeting isLoggedIn={isLoggedIn} /> {button} </div> ); } } ReactDOM.render( <LoginControl />, document.getElementById('root') );
還可以使用短操作符來實現(xiàn)條件篩選,可以用更短的代碼寫出渲染結(jié)果。例如&&來替代if,?:來替代if else, 例如:
function Mailbox(props) { const unreadMessages = props.unreadMessages; return ( <div> <h2>Hello!</h2> {unreadMessages.length > 0 && <h3> You have {unreadMessages.length} unread messages. </h3> } </div> ); } const messages = ['React', 'Re: React', 'Re:Re: React']; ReactDOM.render( <Mailbox unreadMessages={messages} />, document.getElementById('root') );
render() { const isLoggedIn = this.state.isLoggedIn; return ( <div> The user is <b>{isLoggedIn ? 'currently' : 'not'}</b> logged in. </div> ); }
這種跟更大的表達式的寫法也可以,但是不推薦,因為代碼就不是很直觀了。
render() { const isLoggedIn = this.state.isLoggedIn; return ( <div> {isLoggedIn ? ( <LogoutButton onClick={this.handleLogoutClick} /> ) : ( <LoginButton onClick={this.handleLoginClick} /> )} </div> ); }
如果組件有時候需要渲染出來,而有時候不需要渲染出來,在不需要渲染的時候返回null即可。例如:
function WarningBanner(props) { if (!props.warn) { return null; } return ( <div className="warning"> Warning! </div> ); } class Page extends React.Component { constructor(props) { super(props); this.state = {showWarning: true} this.handleToggleClick = this.handleToggleClick.bind(this); } handleToggleClick() { this.setState(prevState => ({ showWarning: !prevState.showWarning })); } render() { return ( <div> <WarningBanner warn={this.state.showWarning} /> <button onClick={this.handleToggleClick}> {this.state.showWarning ? 'Hide' : 'Show'} </button> </div> ); } } ReactDOM.render( <Page />, document.getElementById('root') );
看完了這篇文章,相信你對“React中條件渲染的示例分析”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)站名稱:React中條件渲染的示例分析
文章URL:http://aaarwkj.com/article34/jechse.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供虛擬主機、網(wǎng)站設計公司、網(wǎng)站改版、外貿(mào)網(wǎng)站建設、網(wǎng)站導航、用戶體驗
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)