I have some json data. 3 different objects. I would like to take the data from those objects and present it on the screen. basically each object has a name, summary and icon. so I'd like to go through all 3 of these objects and display on the screen each icon, name and summary. these objects are featured apps. they are displayed in a grid. each app has its own badge. the badge is in the AppBadgeHomePage.js file.
right now nothing is rendering. I get the h3 tag and that's it. there's a div class for "featured" but there is nothing below. it seems the this.state.feat.map is broken. or not working. or maybe it is and there's just nothing getting passed to the app badge. I call the featured apps grid in another file just fine. I'm just wondering if this is one of those cases where I'm too tired and have been staring at this for too long.
the json data looks like this. the icon and summary are in a customData object.
{
name: appName,
customData: {
icon: iconURL,
summary: summary
}
}
Marketplace.js
import AppBadgeHomePage from './AppBadgeHomePage';
import React, { Component } from 'react';
export default class FeaturedGrid extends React.Component {
constructor(props){
super(props);
this.getFeaturedApps = this.getFeaturedApps.bind(this);
let name, summary, icon;
}
state = {
feat: []
}
getFeaturedApps = () => {
fetch(`http://localhost:3001/apps/featured`)
.then(response => response.json())
.then(responseJson => {
this.setState({
feat: responseJson.map(item => ({
name: item.name,
icon: item.customData.icon,
summary: item.customData.summary
}))
})
})
}
render(){
return (
<div>
<h3>Featured Apps</h3>
<div className="featured">
{this.state.feat.map(featured => (
<AppBadgeHomePage
name={featured.name}
icon={featured.customData.icon}
summary={featured.customData.summary}
/>
))}
</div>
</div>
)
}
}
AppBadgeHomePage.js
import React, { Component } from 'react';
export default class AppBadgeHomePage extends React.Component {
render(){
return (
<a className="featured-grid-item-badge" href="www.google.com">
<div className="featured-grid-item">
<img className="featured-appIcon" src={this.props.icon} />
<div>
<p className="featuredTitle">{this.props.name}</p>
</div>
<div>
<p className="badgeSummary">{this.props.summary}</p>
</div>
</div>
</a>
)
}
}
fetchcan reject on network errors, so you should include a.catchto handle promise rejections and thrown errors. Also, you need to first checkresponse.okbefore further accessing into the response object. See Checking that the fetch was successful. Adding some "sad path" handling may help you find whythis.state.featremains an empty array.