[英]Issue with Reacts render method :Giving Parsing error
Im getting the following error Parsing error: Unexpected token, expected ",",for the below code我收到以下错误解析错误:意外的令牌,预期的“,”,对于以下代码
class ProductDetails extends React.Component{
constructor(){
super();
this.state = {timer:0};
}
start = () => {
this.setState({timer:this.state.timer + 1});
}
componentDidMount(){
setInterval(this.start,1000)
}
render(){
var data = [{ _id:12345,
pdtName:"Gaming Laptop",
pdtPrice:35000,
pdtDescription:"An excellent choice for great gaming experience",
img:'images/Laptop.jpg',
rating:5,
isDiscontinued:false
}];
return(
<h3>Timer: {this.state.timer} </h3>
data.map(product =>{
return <Product pimg={product.img} pid = {product._id} pname = {product.pdtName} pprice = {product.pdtPrice} pdesc = {product.pdtDescription}
prating = {product.rating} pisdiscon = {product.isDiscontinued} key={product._id}/>
})
)
}
} }
When I remove当我删除
Thanks谢谢
React component can return only one element. React 组件只能返回一个元素。 You can wrap return inside another element, eg
<div>...</div>
, or use React.Fragment which not adding extra nodes to the DOM.您可以将 return 包装在另一个元素中,例如
<div>...</div>
,或者使用React.Fragment ,它不会向 DOM 添加额外的节点。
return (
<React.Fragment>
<h3>Timer: {this.state.timer} </h3>
{data.map(product => {
return (
<Product
pimg={product.img}
pid={product._id}
pname={product.pdtName}
pprice={product.pdtPrice}
pdesc={product.pdtDescription}
prating={product.rating}
pisdiscon={product.isDiscontinued}
key={product._id}
/>
);
})}
</React.Fragment>
);
render() function should return only one element. render() function 应该只返回一个元素。 In your case it is returning 2 elements which are
<h3> and <Product>
.在您的情况下,它返回 2 个元素,它们是
<h3> and <Product>
。 You can resolve this by using <React.Fragment>
as mentioned above or simply put everything inside <div>
您可以通过使用上面提到的
<React.Fragment>
或简单地将所有内容放入<div>
来解决此问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.