[英]Error: Objects are not valid as a React child (found: object with keys…)
I am trying to loop through the array of objects. 我试图遍历对象数组。 Value of this.state.saveFriendTag or this.props.userTags on console is:
控制台上的this.state.saveFriendTag或this.props.userTags的值为:
State in constructor is: saveFriendTag: this.props.userTags? 构造函数中的状态为:saveFriendTag:this.props.userTags? this.props.userTags: [],
this.props.userTags:[],
Code is: 代码是:
if(this.props.cardData){
if (this.state.saveFriendTag.length == 1) {
taggedFriendsBlue = this.state.saveFriendTag.map((item, index) => {
console.log(item,"item");
return (
<span className="displayedName blue" key={index}>{item.firstname}</span>
)
})
}
This is in return and taggedFriendsBlue is defined in render: 作为回报,在render中定义了aggedFriendsBlue:
<div className="pCard_contentContainer ">
<textarea id="pcardTextarea" type="text" placeholder="Write Description here..." value={this.state.Description} onChange={this.textareaExpansion.bind(this)}></textarea>
<If test={this.state.tagDone == true || this.state.saveFriendTag.length>0}>
<div className="displayNames disp_inliFl">
<span className="pcard_WithOne">-With</span>
<div className="disp_inliFl">
{taggedFriendsBlue}
</div>
</div>
</If>
</div>
Can anybody tell the reason for this console error? 谁能说出此控制台错误的原因? How to correct it?
如何纠正?
It looks like the issue is that you are using Object.keys
on an array. 看起来问题在于您正在数组上使用
Object.keys
。 You can remove that and just use .map
directly. 您可以删除它,而直接使用
.map
。
Additionally, you need to specify a key for the span
. 另外,您需要为
span
指定一个键。
<span key={item.id} ... />
const array = [ { firstName: "test", lastName: "test2" } ]; console.log(Object.keys(array));
As you can see from the code snippet, using Object.keys
on an array will result in the index being passed on each iteration of the map function, instead of the object as you intend. 从代码片段中可以看到,在数组上使用
Object.keys
将导致在map函数的每次迭代中传递索引,而不是您想要的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.