gpt4 book ai didi

javascript - react : unique "key" prop

转载 作者:行者123 更新时间:2023-11-29 16:07:57 25 4
gpt4 key购买 nike

我收到此错误消息,尽管我的列表元素确实有一个键 :(

react.js:18745 警告:数组或迭代器中的每个 child 都应该有一个唯一的“键” Prop 。检查Thread 的渲染方法。

这是我的代码:

render: function () {
var messages = this.state.messagesCache.map(message => {
return (message.own) ? (
<li className="row own" key={message.id}>
<p className="own message-body own-message-background small-5 medium-7 large-9 columns">
{message.body}
</p>
<p className="message-time small-2 large-1 columns">
{getTimePassed(message.timestamp)}
</p>
</li>
) : (
<li className="row foreign" key={message.id}>
<p className="foreign message-body foreign-message-background small-5 medium-7 large-9 columns">
{message.body}
</p>
<p className="message-time small-2 large-1 columns">
{getTimePassed(message.timestamp)}
</p>
</li>
)
})
return this.state.currentThread ? (
<div className="full-width">
<ul className="inline-list uiScrollableArea">
{messages}
</ul>
<form onSubmit={this.handleSubmit} className="row" action="">
<input className="small-12 columns" id={this.state.currentThread} autoComplete="off"
placeholder="input new message"/>
</form>
</div>
) : (
<div>
{"choose a thread to display"}
</div>
)
}

非常感谢您的帮助:)

最佳答案

你确定吗,message.id 始终是唯一的。如果没有,

尝试:

var messages = this.state.messagesCache.map((message,index) => {
return (message.own) ? (
<li className="row own" key={index}>
<p className="own message-body own-message-background small-5 medium-7 large-9 columns">
{message.body}
</p>
<p className="message-time small-2 large-1 columns">
{getTimePassed(message.timestamp)}
</p>
</li>
) : (
<li className="row foreign" key={index}>
<p className="foreign message-body foreign-message-background small-5 medium-7 large-9 columns">
{message.body}
</p>
<p className="message-time small-2 large-1 columns">
{getTimePassed(message.timestamp)}
</p>
</li>
)
})

关于javascript - react : unique "key" prop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36355440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com