反应世界令人沮丧......我需要能够根据某些标准创建标记。例如,我收到一系列物品。我需要检查这些项目,并根据标准,我需要生成不同的标记。因此,例如,我有一个接收项目数组的函数。如何将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