Last active
January 17, 2020 13:39
-
-
Save omosehin/25812b5806ddf324d789054a374101c1 to your computer and use it in GitHub Desktop.
fetching in react
This file contains 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"; | |
class Movie extends Component { | |
constructor() { | |
super(); | |
this.state = { | |
pictures:[], | |
error :null, | |
loading:false | |
}; | |
} | |
componentDidMount() { | |
this.setState({loading:true}) | |
fetch("http://www.omdbapi.com/?i=tt3896198&apikey=3fe2ee61") | |
.then(results => results.json()) | |
.then(res=>{ | |
console.log(res.data) | |
this.setState({pictures: res.data, loading: false})}) | |
.catch(err=>{ | |
console.log(err.data) | |
this.setState({error:err.data,loading:false}); | |
}); | |
} | |
render() { | |
const {pictures, error,loading} =this.state | |
return ( | |
<div> | |
{ loading && <p>Loading...</p>} | |
{!loading && <div className="container2"> | |
<div className="container1"> | |
{ | |
pictures ? pictures.map((pic,index )=>{ | |
return( | |
<div key={index}> | |
<img alt="unable to load" src={pic.Poster} /> | |
</div> | |
) | |
}) :<div>{error}</div> | |
} | |
</div> | |
</div>} | |
</div> | |
); | |
} | |
} | |
export default Movie; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment