3 回答
![?](http://img1.sycdn.imooc.com/54586453000163bd02200220-100-100.jpg)
TA贡献1829条经验 获得超7个赞
您应该使用 map 遍历对象的配置文件数组:
<div className="App">
{this.state.profiles.map((profile, index) =>
<p>{profile.name}
{profile.description}</p>
)}
<br/>
</div>
关于地图功能的更多细节:https : //reactjs.org/docs/lists-and-keys.html
![?](http://img1.sycdn.imooc.com/533e4c1500010baf02200220-100-100.jpg)
TA贡献2065条经验 获得超14个赞
您需要遍历数据(即对象数组)并使用您所需的对象属性呈现它们。
添加一个返回 JSX 以渲染配置文件数据的方法:
renderProfiles = () => {
const profiles = this.state.profiles || [];
const renderProfiles = profiles.map(profile => (
<div key={profile.mainCompanyID}> // pass unique id as a key value
{profile.name}
{profile.description}
</div>
));
return renderProfiles;
};
然后将其添加到您的主要渲染方法中:
render() {
return (
<div className="App">
{this.renderProfiles()}
</div>
);
}
完整的代码和框链接 - https://codesandbox.io/s/elastic-sara-qjc7r
![?](http://img1.sycdn.imooc.com/5458464a00013eb602200220-100-100.jpg)
TA贡献2051条经验 获得超10个赞
您可以创建一个方法并从渲染调用它:
render() {
return (
<div className="App">
{this.renderProfiles()}
</div>
);
}
renderProfiles = () => {
return this.state.profiles.map((profile)=>{
const {name,description} = profile;
return (
<div className="App">
{name}
{description}
</div>
)
})
}
}
添加回答
举报