reactjs - 无法迭代并将嵌套在“帖子”中的“评论”传递给组件
问题描述
我在来自 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
或多态关系。
推荐阅读
- java - 在单独的进程中打开 url
- azure-devops - 为什么 VSTS 构建“Visual Studio 测试”步骤找不到我的 NUnit 测试?
- c++ - (C++) 我的自定义数组无法初始化(编译错误)
- blockchain - 使用 Web3 1.0 调用智能合约方法
- python-3.x - 为什么我们不能简单地打印“print(zip(a,b))”
- pentaho - 合并行(差异)是逐行比较,而不是一行与其他表的整行
- angularjs - 如何在angularjs中使用json在单个tr中打印两个不同的数组元素
- laravel - Laravel postgresql 连接错误。“运营商不存在”
- sockets - 如何在java中保持连接套接字
- php - Wordpress 分页不起作用,它不会移动到下一页