在 React 中,你可以創建不同的組件來封裝各種你需要的行為。然後還可以根據應用的狀態變化只渲染其中的一部分。
React 中的條件渲染和 JavaScript 中的一致,使用 JavaScript 操作符 if 或條件運算符來創建表示當前狀態的元素,然後讓 React 根據它們來更新 UI。
先來看兩個組件:
React 是一個用於構建用戶界面的 JAVASCRIPT 庫。React 主要用於構建UI,很多人認為 React 是 MVC 中的 V(視圖)。React 起源於 Facebook 的內部項目,用來架設 Instagram 的網站,並於 2013 年 5 月開源。React 擁有較高的性能,代碼邏輯非常簡單,越來越多的人已開始關注和使用它。
function UserGreeting(props) {
return ;
}
function GuestGreeting(props) {
return ;
}
歡迎回來!
請先註冊。
我們將創建一個 Greeting 組件,它會根據用戶是否登錄來顯示其中之一:
React 實例
function Greeting(props) {
const isLoggedIn = props.isLoggedIn;
if (isLoggedIn) {
return
; } return
;} ReactDOM.render( // 嘗試修改 isLoggedIn={true}:
, document.getElementById('example'));
元素變量
你可以使用變量來儲存元素。它可以幫助你有條件的渲染組件的一部分,而輸出的其他部分不會更改。
在下面的例子中,我們將要創建一個名為 LoginControl 的有狀態的組件。
它會根據當前的狀態來渲染
或
,它也將渲染前面例子中的
React 實例
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 =
; } else { button =
; } return ( ); }} ReactDOM.render(
{button}
, document.getElementById('example'));
與運算符 &&
你可以通過用花括號包裹代碼在 JSX 中嵌入任何表達式 ,也包括 JavaScript 的邏輯與 &&,它可以方便地條件渲染一個元素。
React 實例
function Mailbox(props) {
const unreadMessages = props.unreadMessages;
return (
);
}
const messages = ['React', 'Re: React', 'Re:Re: React'];
ReactDOM.render(
{unreadMessages.length > 0 && }
Hello!
您有 {unreadMessages.length} 條未讀信息。
, document.getElementById('example'));
在 JavaScript 中,true && expression 總是返回 expression,而 false && expression 總是返回 false。
因此,如果條件是 true,&& 右側的元素就會被渲染,如果是 false,React 會忽略並跳過它。
三目運算符
條件渲染的另一種方法是使用 JavaScript 的條件運算符:
condition ? true : false。
在下面的例子中,我們用它來有條件的渲染一小段文本。
render() { const isLoggedIn = this.state.isLoggedIn; return (
The user is {isLoggedIn ? 'currently' : 'not'} logged in.
); }
同樣它也可以用在較大的表達式中,雖然不太直觀:
render() {
const isLoggedIn = this.state.isLoggedIn;
return (
);
}
{isLoggedIn ? (
) : (
)}
阻止組件渲染
在極少數情況下,你可能希望隱藏組件,即使它被其他組件渲染。讓 render 方法返回 null 而不是它的渲染結果即可實現。
在下面的例子中,
根據屬性 warn 的值條件渲染。如果 warn 的值是 false,則組件不會渲染:
React 實例
function WarningBanner(props) {
if (!props.warn) {
return null;
}
return (
);
}
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 (
);
}
}
ReactDOM.render(
警告!
{this.state.showWarning ? '隱藏' : '顯示'}
, document.getElementById('example'));
組件的 render 方法返回 null 並不會影響該組件生命周期方法的回調。例如,componentWillUpdate 和 componentDidUpdate 依然可以被調用。
本文地址:https://www.linuxprobe.com/react-study-rendering.html
特別聲明:以上內容(如有圖片或視頻亦包括在內)為自媒體平臺「網易號」用戶上傳並發布,本平臺僅提供信息存儲服務。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.