【发布时间】:2021-06-12 05:53:49
【问题描述】:
我在地图中渲染组件,并为我的组件提供称为post 的数据对象。 这很好用。
{feed.map((post, index) =>
(
<PostBasic post={post} key={post.id} creation={creation} />
)
)}
帖子中包含creationDate、description 等信息...
我想在地图中使用之前帖子的creationDate 扩展每个PostBasic。首先我想做这样的事情,但它不起作用,post[index] 或 post[index-1]
{feed.map((post, index) =>
(
<PostBasic post={post} postBefore={post[index-1]creationDate} key={post.id} creation={creation} />
)
)}
如果地图中有 5 个元素,我期待以下输出:
- PostBasic with post={post} and Date of now
- PostBasic with post={post} and creationDate of post[0]
- PostBasic with post={post} and creationDate of post[1]
- PostBasic with post={post} and creationDate of post[2]
- PostBasic with post={post} and creationDate of post[3]
【问题讨论】:
-
您将访问 feed 的
[i-1]- 而不是发布。 -
帖子保存在提要中。我试图更清楚地描述它。我已经在每个 PostBasic 中接收到帖子数据,我只是无法在 PostBasic 中接收到帖子的信息
-
数组
feed是您要映射的对象,post是当前值,index是它的索引位置。我希望如果您正在映射提要,您将访问 feed 的i-1- 这是您正在映射的数组。试试看。