提供的答案没有给我提示我的案例有什么问题。所以我在控制台中收到以下错误:
react-jsx-dev-runtime.development.js?bfcc:117 Warning: Each child in a list should have a unique "key" prop.
Check the render method of `Feed`. See https://reactjs.org/link/warning-keys for more information.
Run Code Online (Sandbox Code Playgroud)
这是我在返回语句中的代码:
return (
<>
<Header></Header>
<div className={hfstyles.feed} style={{ 'maxWidth': '980px;', 'textAlign': 'center', 'margin': '0 auto', 'padding': '5em 0' }}>
{feed.map((post, index) => {
const postBefore = index == 0 ? new Date() : feed[index - 1].createdAt;
return randomIndex == index ? (
<>
<PostBasic
post={post}
postBefore={postBefore}
key={post.id}
/>
<RandomMessage/>
</>)
:
(<PostBasic
post={post}
postBefore={postBefore}
key={post.id}
/>
);
})}
{feedEnd == true && (
<p>Thats it</p>
)}
</div>
<Footer sticky={false}></Footer>
</>
);
Run Code Online (Sandbox Code Playgroud)
我还尝试key向 提供属性,<RandomMessage/> component并尝试将密钥从数据库 id 更改post.id为index地图的 ,而不进行任何更改
Rah*_*rma 37
您必须将关键道具传递给fragment(<>),而不是传递给PostBasic。
像这样使用,
改变这个
(
<>
<PostBasic post={post} postBefore={postBefore} key={post.id} />
<RandomMessage />
</>
);
Run Code Online (Sandbox Code Playgroud)
到
(<React.Fragment key={post.id}>
<PostBasic post={post} postBefore={postBefore} />
<RandomMessage />
</React.Fragment>
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
48958 次 |
| 最近记录: |