index.jsx 669 B

123456789101112131415161718192021222324252627282930313233
  1. import React from 'react';
  2. import {Link} from 'react-router-dom';
  3. import {Pokemon} from '../../../api';
  4. class PokemonIndex extends React.Component {
  5. state = {
  6. pokemon: [],
  7. };
  8. async componentDidMount() {
  9. try {
  10. const data = await Pokemon.index();
  11. this.setState({pokemon: data.data});
  12. } catch (err) {
  13. console.log(JSON.stringify(err, null, 2));
  14. }
  15. }
  16. render() {
  17. const pokemon = this.state.pokemon.map((pkmn, i) => {
  18. return (
  19. <li key={pkmn.id}>
  20. <Link to={`/pokemon/${pkmn.id}`}>{pkmn.nickname}</Link>
  21. </li>
  22. );
  23. });
  24. return <ul>{pokemon}</ul>;
  25. }
  26. }
  27. export default PokemonIndex;