I'm a beginner in react and I have the following issues:
Im using an API that has more than 60 characters, my issue is that I want to make sure all the characters of my API render up without me having to manually add each number.({this.state.people[0].name}, ..., {this.state.people[n].name}).
This issue also made me do the API call two times because the API gives only 10 results per call. they next for page 2.
I created a new call with ?=page2 but this is a very tedious way of doing it because I would have to add 10 different API calls to add all the characters. I thought about doing a for loop but I dont really know how to approach this.
class Api extends Component {
state ={
people:[
{name:'Title'},
{name:'Title'},
{name:'Title'},
{name:'Title'},
{name:'Title'},
{name:'Title'},
{name:'Title'},
{name:'Title'},
{name:'Title'},
{name:'Title'},
{name:'Title'},
{name:'Title'},
{name:'Title'},
],
people2:[
{name:'Title'},
{name:'Title'},
{name:'Title'},
{name:'Title'},
{name:'Title'},
{name:'Title'},
{name:'Title'},
{name:'Title'},
{name:'Title'},
{name:'Title'},
{name:'Title'},
{name:'Title'},
{name:'Title'},
]
}
componentDidMount() {
axios.get('https://swapi.co/api/people/')
.then(starwars => {
this.setState({
people: starwars.data.results
})
console.log(starwars.data.results)
})
axios.get('https://swapi.co/api/people/?page=2')
.then(starwars2 => {
this.setState({
people2: starwars2.data.results
})
console.log(starwars2.data.results)
})
.catch(error => {
console.log(error);
});
}
render(){
return (
<div>
<Cards title={this.state.people[0].name} />
<Cards title={this.state.people[1].name} />
<Cards title={this.state.people[2].name} />
<Cards title={this.state.people[3].name} />
<Cards title={this.state.people[4].name} />
<Cards title={this.state.people[5].name} />
<Cards title={this.state.people[6].name} />
<Cards title={this.state.people[7].name} />
<Cards title={this.state.people[8].name} />
<Cards title={this.state.people[9].name} />
<Cards title={this.state.people2[0].name} />
<Cards title={this.state.people2[1].name} />
<Cards title={this.state.people2[2].name} />
<Cards title={this.state.people2[3].name} />
<Cards title={this.state.people2[4].name} />
<Cards title={this.state.people2[5].name} />
<Cards title={this.state.people2[6].name} />
<Cards title={this.state.people2[7].name} />
<Cards title={this.state.people2[8].name} />
<Cards title={this.state.people2[9].name} />
</div>)
}
}
export default Api;
Make the API call for the 60 characters in one call.