我正在關注Thinking in React Tutorial,並在下面的第2部分中看到,通過JSX將var rows
內插到標記中。 JSX如何知道分解數組中的每個標記項?因爲對我來說,{rows}
的插值將簡單地返回標記的數組,而不是逐個佈置的每個標記行。所以,我看到它回到[rowA, rowB, ...]
代替<rowA /> <rowB /> ...
React的JSX如何通過插值打破數組?
var ProductTable = React.createClass({
render: function() {
var rows = [];
var lastCategory = null;
this.props.products.forEach(function(product) {
if (product.category !== lastCategory) {
rows.push(<ProductCategoryRow category={product.category} key={product.category} />);
}
rows.push(<ProductRow product={product} key={product.name} />);
lastCategory = product.category;
});
return (
<table>
<thead>
<tr>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody>{rows}</tbody>
</table>
);
}
});
[1]: https://facebook.github.io/react/docs/thinking-in-react.html
所以{}的花括號不僅僅意味着評估這個表達式,而且還渲染它裏面的內容?看着這個文檔我找不到任何東西。如果我使用React元素的鍵給它一個對象,它是否也以相同的方式工作? – mangocaptain
我不這麼認爲,讓我更新答案... –