【发布时间】:2018-03-19 06:34:13
【问题描述】:
未捕获的错误:对象作为 React 子项无效(找到:带有键 {titlesCollection} 的对象)。如果您打算渲染一组子项,请改用数组
我在尝试渲染对象数组时遇到此错误,我在这里做错了什么?
import * as React from 'react';
import * as FontAwesomeIcon from 'react-fontawesome';
const data = {
otherTitles:[
{
titleHeading:"X",
titles:["A","B","C","D"]
},
{
titleHeading:"Y Z",
titles:["E","F","G","H"]
}
]
}
export class OtherTitlesCollection extends React.Component{
render() {
const titlesCollection = data.otherTitles.map((othertitle)=>{
let dataId = othertitle.titleHeading.replace(' ','');
return(
<div key={dataId}>
<div>
<FontAwesomeIcon name='plus-circle' data-toggle="collapse"
data-target={"#"+dataId} role="button" aria-expanded="false" aria-controls={dataId}/>
<label>{othertitle.titleHeading}</label>
</div>
<div className="collapse" id ={dataId}>
{
othertitle.titles.map((title)=>{
return(<div key={title} style={{margin: "1px auto 1px 10px"}}>{title}</div>);
})
}
</div>
</div>
)
});
return (
{titlesCollection}
);
}
};
【问题讨论】:
标签: javascript reactjs