Javascript 函数不';我什么也不做
基本上,我将一个对象数组作为参数发送到函数中,但函数什么也不做。当我想打印出我的对象数组时,我可以在控制台上显示所有项目,但是,我不能渲染它们 renderComment()函数不工作或不返回任何内容。另外,您还可以仔细查看我的render方法,您将看到在控制台中打印出renderComment预期输出的注释代码Javascript 函数不';我什么也不做,javascript,reactjs,Javascript,Reactjs,基本上,我将一个对象数组作为参数发送到函数中,但函数什么也不做。当我想打印出我的对象数组时,我可以在控制台上显示所有项目,但是,我不能渲染它们 renderComment()函数不工作或不返回任何内容。另外,您还可以仔细查看我的render方法,您将看到在控制台中打印出renderComment预期输出的注释代码 class DishDetails extends Component { renderDish(dish) { if (dish) {
class DishDetails extends Component {
renderDish(dish) {
if (dish) {
return (
<div>
<Card className="col-12 col-md-5 m-1">
<CardImg width="100%" src={dish.image} alt={dish.name} />
<CardBody>
<CardTitle>{dish.name}</CardTitle>
<CardText>{dish.description}</CardText>
</CardBody>
</Card>
</div>
)
} else {
return (
<div></div>
)
}
}
renderComments(comments) {
comments.map(comment => {
return (
<li key={comment.id}>
<p>{comment.comment}</p>
<p>{comment.author}</p>
</li>
)
})
}
render() {
const selected = this.props.selectedDish;
/* if(selected) {
this.props.selectedDish.comments.map(comment => {
console.log(comment.comment)
})
} */
return (
<div>
{selected &&
<div>
{this.renderDish(this.props.selectedDish)}
{this.renderComments(this.props.selectedDish.comments)}
</div>
}
</div>
)
}
}
export default DishDetails
类详细信息扩展组件{
renderDish(碟形){
如果(盘){
返回(
{dish.name}
{dish.description}
)
}否则{
返回(
)
}
}
renderComments(注释){
comments.map(comment=>{
返回(
{comment.comment}
{comment.author}
)
})
}
render(){
const selected=this.props.selectedDish;
/*如果(选定){
this.props.selectedDish.comments.map(comment=>{
console.log(comment.comment)
})
} */
返回(
{选定&&
{this.renderDish(this.props.selectedDish)}
{this.renderComments(this.props.selectedDish.comments)}
}
)
}
}
导出默认详细信息
由于函数中没有return
语句,因此renderComment
函数不返回任何内容。映射中有一个返回
(这仍然是必需的),但这不是函数的返回
要解决此问题,请在renderComment
顶部添加一个return
,如下所示:
renderComment(comments) {
return comments.map(comment => {
return (
<li key={comment.id}>
<p>{comment.comment}</p>
<p>{comment.author}</p>
</li>
)
}
}
renderComment(注释){
返回comments.map(comment=>{
返回(
{comment.comment}
{comment.author}
)
}
}
由于函数中没有return
语句,因此renderComment
函数不返回任何内容。在映射中有一个return
(仍然需要),但这不是函数的return
要解决此问题,请在renderComment
顶部添加一个return
,如下所示:
renderComment(comments) {
return comments.map(comment => {
return (
<li key={comment.id}>
<p>{comment.comment}</p>
<p>{comment.author}</p>
</li>
)
}
}
renderComment(注释){
返回comments.map(comment=>{
返回(
{comment.comment}
{comment.author}
)
}
}
它不返回任何内容,因为其中没有return语句。它不返回任何内容,因为其中没有return语句。