Created
January 17, 2020 13:44
-
-
Save omosehin/b385e6db78c3fa76b17b46c8077bb4aa to your computer and use it in GitHub Desktop.
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); | |
this.setState({ pictures: res.data, loading: false }); | |
}) | |
.catch(err => { | |
console.log(err); | |
this.setState({ error: err.data, loading: false }); | |
}); | |
} | |
render() { | |
const { pictures, error, loading } = this.state; | |
return ( | |
<div> | |
{loading ? ( | |
<p>Loading...</p> | |
) : ( | |
<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 && "Error loading picture"}</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