I am trying to get a list of movies from an array that I have. I am looping over the array to put the title and poster on the screen. But even though the loop only happens once, it keeps making the same three api calls over and over again. I am really new to React and thought that it should only make a new call if something changes?
import React from 'react';
import $ from 'jquery';
var movieArray = ['Tarzan','1o1uyry29ddz', 'tt0120855',
'Close Encounters Of The Third Kind', 'g3hl6ewv9b7h', 'tt0075860',
'10,000 BC', 'tngmycvr418q', 'tt0443649'
];
export class Home extends React.Component {
constructor(props) {
super(props);
this.state = {
name: '',
poster: '',
backdrop: '',
genre: '',
release: '',
description: ''
};
}
componentDidUpdate(id){
let self = this;
let url = 'https://api.themoviedb.org/3/find/'
let apiKey = '?api_key=<my-api-key>'
let language = '&language=en-US'
let imdb = '&external_source=imdb_id'
$.ajax({
type: 'GET',
url: url + id + apiKey + language + imdb
})
.done(function(data) {
self.setState({
name: data.title,
poster: data.poster_path,
backdrop: data.backdrop_path,
genre: data.genre_ids,
release: data.release_data,
description: data.overview
});
})
.fail(function(jqXhr) {
console.log('Failed to Connect to TMDB Server');
});
}
movieLoad(){
var arrayLength = movieArray.length;
var movieList = [];
var url = "http://image.tmdb.org/t/p/w185//"+this.poster ;
for (var i = 0; i < arrayLength-2; i++) {
this.componentDidUpdate(movieArray[i+2]);
console.log(this.title);
movieList.push(
<div>
<h1>{this.title}</h1>
<img src= {url} />
</div>
);
i = i + 2;
}
return movieList;
}
render() {
return (
<div>
{this.movieLoad()}
</div>
);
}
}
export default Home;