import React from 'react';
import { View, Text } from 'react-native';
import axios from 'axios';
import Pokemon from './Pokemon';
class PokemonList extends React.Component {
constructor(props) {
super(props);
this.state = {
pokemons: [],
isLoading: true,
error: null,
};
}
componentDidMount() {
axios
.get('https://pokeapi.co/api/v2/pokemon?limit=151')
.then((response) => {
const pokemons = response.data.results.map((result, index) => {
const id = index + 1;
const imageUrl = `https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${id}.png`;
return new Pokemon(result.name, imageUrl, []);
});
this.setState({ pokemons, isLoading: false });
})
.catch((error) => {
this.setState({ error, isLoading: false });
});
}
render() {
const { pokemons, isLoading, error } = this.state;
if (isLoading) {
return <Text>Loading...</Text>;
}
if (error) {
return <Text>Error: {error.message}</Text>;
}
return (
<View>
{pokemons.map((pokemon) => (
<Text key={pokemon.name}>{pokemon.name}</Text>
))}
</View>
);
}
}
export default PokemonList;