反應世界令人沮喪......我需要能夠根據某些標準創建標記。例如,我收到一系列物品。我需要檢查這些項目,並根據標準,我需要生成不同的標記。因此,例如,我有一個接收項目數組的函數。如何將JSX元素連接成一個數組?
processItems(itemArray) {
// Create an empty array that will hold the final JSX output.
let buffer = []
// Somehow push the first required markup into the buffer.
buffer.push(<div className"container flex center">);
... here we do a switch statement that evaluates each item in the 'itemArray' argument and based on that I create different <div className="XYZ">{...put stuff here...}</div> markup, which I will push into the buffer - which will contain the final JSX output...
// Now I close the initial container element
buffer.push(</div>);
// And return the buffer for display inside the render() function
return buffer;
}
的問題是,我不能簡單地做「的Array.push()」個人標記添加到一個數組,因爲反應不喜歡它由於某種原因,我會剛剛結束了jibberish東西,得到顯示。
任何想法我怎麼可能做到這一點?
謝謝。
*「我不能簡單地做‘的Array.push()’個人標記添加到一個數組,因爲反應不喜歡它出於某種原因,「*這是你無法創建一半* DOM元素*的原因。 JSX不是連接* strings *來構建* HTML *,而是創建最終呈現給DOM元素的組件。如果你看看JSX被轉換成什麼,它將有希望變得更有意義。粘貼:'
你有沒有得到這個工作?我正在嘗試做類似的事情。在解析器內部,根據JSON對象的內容構建JSX。 – Addinall