我的“评论”嵌套在来自Rails的名为“目击”的帖子中。我将'目击'阵列作为支柱传递给SightingContainer
,然后传递给SightingCard
。一切正常。然后我需要在同一张卡片中添加“评论”(评论是可评论的)。现在我将它作为一个单独的分支从App中处理,并将一系列'目击'(有嵌套的评论)发送到CommentsContainer
然后发送到CommentsCard
。在CommentContainer
中,我正在进行2次迭代以深入查看“评论”。
我是一个React新手,所以我可能没有正确实现这一点。
我的ComponentContainer
接受阵列props.sightings
:
import React from 'react';
import { Comment } from 'semantic-ui-react'
import CommentCard from '../components/CommentCard'
const CommentsContainer = props => {
return (
<div>
{props.sightings && props.sightings.map(sighting => {
return sighting.comments.map((comment, idx) => {
return <CommentCard key={idx} comment={comment} editComment={props.editComment}
likeComment={props.likeComment} addComment={props.addComment} currentUser={props.currentUser} deleteComment={props.deleteComment}/>
})
})
}
</div>
)
};
export default CommentsContainer
我得到的错误是:
×
←→1 of 3 errors on the page
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
如果有人能告诉我更好的实施方法,请指教!
因此,似乎整体React不能很好地嵌套数据。更好的是在后端处理它并将其作为非嵌套数据发送,即使它具有belongs_to
或多态关系。