Created
October 11, 2018 16:11
-
-
Save prof3ssorSt3v3/4d3ef51650cc89c557842119b7b6ae8b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {Component} from 'react'; | |
import '../App.css'; | |
import Header from './Header'; | |
export default class Home extends Component{ | |
constructor(){ | |
super(); | |
this.state = { | |
list: [], | |
error: null | |
} | |
} | |
buildList =(data)=>{ | |
console.log(data); | |
this.setState({list: data}) | |
} | |
componentDidMount(){ | |
console.log('did mount') | |
let url = 'https://prof3ssorst3v3.github.io/media-sample-files/products.json'; | |
fetch(url) | |
.then(response => response.json()) | |
.then(this.buildList) | |
.catch(error => { | |
this.setState({error}); | |
}) | |
} | |
render(){ | |
console.log('render') | |
return ( | |
<div> | |
<h1>This is HOME</h1> | |
<Header /> | |
<ul> | |
{ | |
this.state.list.length === 0 && | |
<li>Sorry No data available</li> | |
} | |
{ this.state.list.length > 0 && | |
this.state.list.map( (item) => ( | |
<li key={item.id} id={item.id}>{item.title + " " + item.price}</li> | |
)) | |
} | |
</ul> | |
{this.state.error && | |
<h3>{this.state.error}</h3> | |
} | |
</div> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment