javascript - 从反应组件呈现数据时遇到问题 - 建议?
问题描述
我正在做一个简单的反应项目,并试图创建一个产品表及其各自的价格。在我的ProductRow
组件内部,我使用该forEach()
方法循环遍历我的产品数据并返回产品的名称,然后应该在父ProductTable
组件中呈现这些名称 - 除非它不是。有什么建议么?代码如下:
class FilterableProductTable extends React.Component{
render(){
return(
<div>
<SearchBar/>
<ProductTable products = {this.props.products}/>
</div>
)
}
}
class SearchBar extends React.Component{
render(){
return(
<div>
<input type = 'text' placeholder = 'Search'/>
</div>
)
}
}
class ProductTable extends React.Component{
render(){
return(
<div>
<table>
<tr>
<th>name</th>
<th>price</th>
</tr>
<ProductRow products = {this.props.products}/>
</table>
</div>
)
}
}
class ProductRow extends React.Component{
render(){
const items = this.props.products
return(
<tr>
{
items.forEach(x=> {
return <td>{x.name}</td>
})
}
</tr>
)
}
}
const PRODUCTS = [
{category: 'Sporting Goods', price: '$49.99', stocked: true, name: 'Football'},
{category: 'Sporting Goods', price: '$9.99', stocked: true, name: 'Baseball'},
{category: 'Sporting Goods', price: '$29.99', stocked: false, name: 'Basketball'},
{category: 'Electronics', price: '$99.99', stocked: true, name: 'iPod Touch'},
{category: 'Electronics', price: '$399.99', stocked: false, name: 'iPhone 5'},
{category: 'Electronics', price: '$199.99', stocked: true, name: 'Nexus 7'}
];
ReactDOM.render(
<FilterableProductTable products={PRODUCTS} />,
document.getElementById('container')
);
解决方案
推荐阅读
- docker - dotnet run 在正在运行的 Docker 容器中挂起
- ansible - 具有限制性权限的 Ansible sudo
- javascript - 如何将数据作为对象插入嵌套对象数组中,而我不知道它的深度级别
- php - 从外部 URL 获取缩略图
- html - Python 仅提取 for 循环中每第 n 次出现的第一个 href 链接
- configuration - 无法让 TeamCity 使用 C# 7.1 - 但使用 msbuild 手动工作
- css - 我找不到 CSS 位置值
- php - 查找人口最多的年份(最有效的解决方案)
- c++ - C++ - “!pointer”和“pointer == nullptr”之间的区别?
- parsing - 使用 clj.http 时在 clojure 中解析地图的最佳方法是什么?