新聞中心
條件渲染可以將一個(gè)數(shù)組內(nèi)的所有數(shù)據(jù)依次展示在界面上,常用場(chǎng)景:文字列表、商品列表。React 中的條件渲染就和在 JavaScript 中的條件語(yǔ)句一樣。使用 JavaScript 操作符如 if 或者 條件操作符 來(lái)創(chuàng)建渲染當(dāng)前狀態(tài)的元素,并且讓 React 更新匹配的 UI

先來(lái)看兩個(gè)組件:
function UserGreeting(props) {
return 歡迎回來(lái)!
;
}
function GuestGreeting(props) {
return 請(qǐng)先注冊(cè)。
;
}
我們將創(chuàng)建一個(gè) Greeting 組件,它會(huì)根據(jù)用戶是否登錄來(lái)顯示其中之一:
React 實(shí)例
function Greeting(props) {
const isLoggedIn = props.isLoggedIn;
if (isLoggedIn) {
return
;
}
return
;
}
ReactDOM.render(
// 嘗試修改 isLoggedIn={true}:
false} />, document.getElementById(
'example') );
元素變量
你可以使用變量來(lái)儲(chǔ)存元素。它可以幫助你有條件的渲染組件的一部分,而輸出的其他部分不會(huì)更改。 在下面的例子中,我們將要?jiǎng)?chuàng)建一個(gè)名為 LoginControl 的有狀態(tài)的組件。 它會(huì)根據(jù)當(dāng)前的狀態(tài)來(lái)渲染 或 ,它也將渲染前面例子中的 。
React 實(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 =
;
} else {
button =
;
}
return (
{button}
);
}
}
ReactDOM.render(
,
document.getElementById('example')
);
與運(yùn)算符 &&
你可以通過(guò)用花括號(hào)包裹代碼在 JSX 中嵌入任何表達(dá)式 ,也包括 JavaScript 的邏輯與 &&,它可以方便地條件渲染一個(gè)元素。
React 實(shí)例
function Mailbox(props) {
const unreadMessages = props.unreadMessages;
return (
Hello!
{unreadMessages.length > 0 &&
您有 {unreadMessages.length} 條未讀信息。
}
);
}
const messages = ['React', 'Re: React', 'Re:Re: React'];
ReactDOM.render(
,
document.getElementById('example')
);
在 JavaScript 中,true && expression 總是返回 expression,而 false && expression 總是返回 false。 因此,如果條件是 true,&& 右側(cè)的元素就會(huì)被渲染,如果是 false,React 會(huì)忽略并跳過(guò)它。
三目運(yùn)算符
條件渲染的另一種方法是使用 JavaScript 的條件運(yùn)算符:
condition ? true : false。
在下面的例子中,我們用它來(lái)有條件的渲染一小段文本。
render() { const isLoggedIn = this.state.isLoggedIn; return (
The user is {isLoggedIn ? 'currently' : 'not'} logged in.
); }
同樣它也可以用在較大的表達(dá)式中,雖然不太直觀:
render() {
const isLoggedIn = this.state.isLoggedIn;
return (
{isLoggedIn ? (
) : (
)}
);
}
阻止組件渲染
在極少數(shù)情況下,你可能希望隱藏組件,即使它被其他組件渲染。讓 render 方法返回 null 而不是它的渲染結(jié)果即可實(shí)現(xiàn)。 在下面的例子中, 根據(jù)屬性 warn 的值條件渲染。如果 warn 的值是 false,則組件不會(huì)渲染:
React 實(shí)例
function WarningBanner(props) {
if (!props.warn) {
return null;
}
return (
"warning"> 警告!
);
}
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 (
{this.state.showWarning ? '隱藏' : '顯示'}
);
}
}
ReactDOM.render(
,
document.getElementById('example')
);
組件的 render 方法返回 null 并不會(huì)影響該組件生命周期方法的回調(diào)。例如,componentWillUpdate 和 componentDidUpdate 依然可以被調(diào)用。
網(wǎng)站題目:React條件渲染
轉(zhuǎn)載來(lái)源:http://m.fisionsoft.com.cn/article/cdogdhd.html


咨詢
建站咨詢
