我已經在React組件中渲染。如果條件滿足,我想做點什麼,在這種情況下,當鼠標懸停在圖標上時顯示一條消息,否則我不希望它做任何事情。使用React render不返回任何內容
我嘗試了if條件,但它不起作用。這是我的代碼:
render() {
const text= this.checkSomething();
return (
{
if (text.length > 0) {
<ActionBar>
<div>
<Icon type="..."/>
//do something here
</div>
</ActionBar>
}
}
,但我得到了以下錯誤:
A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.
我知道,我返回的東西,沒有按」存在,如果text.length == 0
,但有什麼辦法,使工作,像如果條件不符合,不要返回任何東西?
的可能的複製[如果速記內聲明,如果聲明】(https://stackoverflow.com/questions/42686424/if-statement-within-shorthand-if-statement) –
@ ShubhamKhatri非常好的答案。你有錯字:如果統計:) –
@VedranMaricevic,謝謝,修正了錯字:) –