如何在 React 中将用户连接到 post?
how do I connect a user to post in React?
如何将用户连接到他们的 post? post 有 post 发表评论的用户 ID,但我该如何映射 posts?
这是我的post地图
{post.map(post => (
<Post key={post.id} post={post} />
))}
fname 和 address 来自用户,post 来自 posts 的列表
和 post 文件:
{fname}
{address}
{post}
所以我有 post 列表和用户列表,它们是分开的
但是 post 具有 post 编辑它的用户 ID,但我如何连接它们?
通过 post 映射并且当来自用户的 userid 等同于来自 post 的 userid 过滤它或什么?
我如何通过这种关系将他们联系起来?
我想要的东西,但它给出了一个错误 user.post is undefined
{post.map(post => (
<Post
key={post.id}
food={post}
user={user.filter(
user =>
user.post.userid)}
/>
))}
因为 post 有 userid
而用户有 id
那么你可以用这样的方法解决它:
{post.map((post) => (
<Post
key={post.id}
food={post}
user={user.find((u) => u.id === post.userid)}
/>
))}
当我在 post 标签中为用户设置如下值时,它对我有效。
<Post
key={post.id}
food={post}
user={users.find(
user =>
post.userid === user.id //if you have an users array here you should use find instead of filter and then try to match the post.userid with user.id.
)}
/>
如何将用户连接到他们的 post? post 有 post 发表评论的用户 ID,但我该如何映射 posts?
这是我的post地图
{post.map(post => (
<Post key={post.id} post={post} />
))}
fname 和 address 来自用户,post 来自 posts 的列表 和 post 文件:
{fname}
{address}
{post}
所以我有 post 列表和用户列表,它们是分开的 但是 post 具有 post 编辑它的用户 ID,但我如何连接它们? 通过 post 映射并且当来自用户的 userid 等同于来自 post 的 userid 过滤它或什么? 我如何通过这种关系将他们联系起来?
我想要的东西,但它给出了一个错误 user.post is undefined
{post.map(post => (
<Post
key={post.id}
food={post}
user={user.filter(
user =>
user.post.userid)}
/>
))}
因为 post 有 userid
而用户有 id
那么你可以用这样的方法解决它:
{post.map((post) => (
<Post
key={post.id}
food={post}
user={user.find((u) => u.id === post.userid)}
/>
))}
当我在 post 标签中为用户设置如下值时,它对我有效。
<Post
key={post.id}
food={post}
user={users.find(
user =>
post.userid === user.id //if you have an users array here you should use find instead of filter and then try to match the post.userid with user.id.
)}
/>