简体   繁体   English

错误:对象作为React子对象无效(找到:带有键的对象…)

[英]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.

相关问题 React,错误:对象作为 React 子项无效(找到:object 和键 {data}) - React , Error: Objects are not valid as a React child (found: object with keys {data}) 错误:对象作为 React 子对象无效(发现:object 键为 {low, high}) - Error: Objects are not valid as a React child (found: object with keys {low, high}) 错误:对象作为 React 子项无效(找到:具有键 {$numberDecimal} 的对象) - Error: Objects are not valid as a React child (found: object with keys {$numberDecimal}) 错误:对象作为 React 子对象无效(找到:带有键 {zip} 的对象) - Error: Objects are not valid as a React child (found: object with keys {zip}) '错误:对象作为 React 子级无效(找到:object 和键 {results,info})' - 'Error: Objects are not valid as a React child (found: object with keys {results, info})' 对象作为React子对象无效(找到:带有键{…}的对象) - Objects are not valid as a React child (found: object with keys {…}) 对象作为 React 子对象无效(找到:带有键的对象...) - Objects are not valid as a React child (found: object with keys…) 对象无效作为React子对象(找到:带有键的对象) - Objects are not valid as a React child (found: object with keys) 对象作为 React 子项无效(找到:object,键为 {}) - Objects are not valid as a React child (found: object with keys {}) 反应本机自动完成输入错误:对象作为反应孩子无效(找到:带键的对象) - React Native Autocomplete input error : Objects are not valid as a react child (found: object with keys)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM