Skip to content

Instantly share code, notes, and snippets.

@omosehin
Created January 17, 2020 13:44
Show Gist options
  • Save omosehin/b385e6db78c3fa76b17b46c8077bb4aa to your computer and use it in GitHub Desktop.
Save omosehin/b385e6db78c3fa76b17b46c8077bb4aa to your computer and use it in GitHub Desktop.
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