1
all right?
I’m starting now in the world of React and I have this little problem in question. How do I specify the consumption of a specific Array with React?
For example:
"count": 1118,
"next": "https://pokeapi.co/api/v2/pokemon?offset=20&limit=20",
"previous": null,
"results": [
{
"name": "bulbasaur",
"url": "https://pokeapi.co/api/v2/pokemon/1/"
},
{
"name": "ivysaur",
"url": "https://pokeapi.co/api/v2/pokemon/2/"
},
{
"name": "venusaur",
"url": "https://pokeapi.co/api/v2/pokemon/3/"
}
....
I can already consume all the data in the array, however, how do I get only 1? Like, how do I get only the:
"name": "bulbasaur",
"url": "https://pokeapi.co/api/v2/pokemon/1/"
i already gave a console.log(Response.data.Results[0]) and showed me exactly what I wanted, but whenever I do so in this.setState({Pokemon: Response.data.Results}) the . map gives an error saying that it is not a function and saw that . map can only work with array...
import React, { Component } from 'react';
import axios from 'axios';
const api = axios.create({
baseURL: 'https://pokeapi.co/api/v2/pokemon'
})
class App extends Component{
state={
pokemon: [],
}
async componentDidMount(){
const response = await api.get('');
this.setState({pokemon: response.data.results})
}
render() {
const { pokemon } = this.state;
return (
<div>
<h1>Pkemon</h1>
{pokemon.map(pk => (
<li key={pk}>
<h2>{pk.name}</h2>
<h2>{pk.url}</h2>
</li>
))}
</div>
);
};
};
export default App;
The first thing I noticed is that it doesn’t exist
response.data.result
- according to the example of return you put, would beresponse.results
.– BrTkCa