I'm trying to get information from a video game API and put it in my list. However, the API is divided in multiple pages (which I don't know how many there are).
I tried this solution: How to fetch data over multiple pages?
Then I tried another solution (code snippet below updated), but it's giving me an error:
Maximum Update Depth Exceeded...
Probably because it never stops updating my 'currentPage' variable
After hours of debugging I gave up.
Here is my file:
import { Card, CardItem } from "native-base";
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
isLoading: true,
dataSource: [],
currentPage: 1
};
}
getUserFromApi = () => {
return fetch('https://api.rawg.io/api/games?page=' + this.state.currentPage +'&platforms=18', {
"method": "GET",
"headers": {
"x-rapidapi-host": "rawg-video-games-database.p.rapidapi.com",
"x-rapidapi-key": "495a18eab9msh50938d62f12fc40p1a3b83jsnac8ffeb4469f"
}
})
.then(response => response.json())
.then(responseJson => {
this.setState({
isLoading: false,
dataSource: this.state.dataSource.concat(responseJson.results)
});
})
.catch(error => console.log(error));
};
componentDidMount() {
this.getUserFromApi();
}
render() {
const { isLoaded, items } = this.state;
if (this.state.isLoading) {
return (
<View style={styles.progress}>
<ActivityIndicator size="large" color="#01CBC6" />
</View>
);
}
return (
<FlatList
data={this.state.dataSource}
onEndReached={ this.setState({ currentPage: this.state.currentPage + 1 }) }