1
Basically I have this method that takes some information from the API and stores it in a state variable (date: []), but since there are several pages, I would like to add the results in this variable to present, after all the requests, all results, but I don’t know how to do it.
makeRemoteRequest = (page) =>{
const url = `http://api.themoviedb.org/3/movie/popular?api_key=<<api_key>>&page=${page}`;
this.setState({loading: true});
fetch(url)
.then(res => res.json())
.then(res => {
this.setState({
data:res.results,
error: res.error || null,
loading: false,
})
this.arrayholder = res.results;
})
.catch(error=>{
this.setState({error, loading:false});
})
}
The state
:
state = {
loading: false,
data: [],
error: null,
page: 1,
}
I have tried to use another variable to control, but I need an object, because to show the final results I will need all the data of the API result.
how many pages does this request have? that’s not very nice to do if the number of pages is large.
– novic
500 pages. But I don’t intend to catch them all. Only 2 or 3.
– Diego Cândido
then one can be done for only, but your doubt is how to aggregate the results, how to join the results (is a list of values neh, ie an array)
– novic
That. Technically I have already managed to do this, but when I go to arrays, I can only take the title (Response.data.title) and convert to a string that will stay in the array. But I would also need to take other values, like Sponse.data.id, etc..
– Diego Cândido