0
我可以創建使用return()
兩種顯示2個const
包含數字作爲值輸出:如何調用返回)VAR(
return (
<div>
<span>{const_one}</span> {const_two}
</div>
)
如果隨機布爾var statement = true
我要輸出
return (
<div>
{const_one}
</div>
)
每當我在return()
內使用if()
函數時,都會出現錯誤。我怎樣才能做到這一點?我試圖創建一個const
檢查狀態並創建另一個const
,包括輸出:
const statement = false;
// or
const statement = true;
let customOutput = '<span>{const_one}</span> {const_two}';
if (statement === true) customOutput = const_one;
return (
<div>
{customOutput}
</div>
)
在這個例子中const_one
或const_two
標籤顯示爲HTML文本,而不是顯示它們的值(這實際上是數字):
<span>{const_one}</span> {const_two}
而不是<span>24</span> 36
。
如何更改標記以顯示內部的{customOutput}
有沒有在你的代碼中的語法錯誤?我收到了一個錯誤,並將其應用於我的更復雜的代碼中,該代碼表示「解析錯誤:意外的令牌」。它的寫法如下:'{pastDate ?數據 : {item.teams &&(item.teams.length)}/ {data}}' –
它像在你的例子中那樣在'span'中包裝整行時起作用。爲什麼會出現這種情況,因爲整個語句被封裝在'div'中,並且'const_one'在沒有被封裝的情況下工作? –
這是JSX的限制。如你所知,在瀏覽器能夠執行之前,JSX必須被編譯爲'React.createElement'調用。如果刪除外部跨度,則生成的JavaScript代碼將包含錯誤。你可以在這裏閱讀更多關於它的信息:https://facebook.github.io/react/docs/jsx-in-depth.html – amann