index.tsx 891 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import React from 'react';
  2. import {Link} from 'react-router-dom';
  3. import {Pokemon} from '../../../api';
  4. interface Pokemon {
  5. id: number;
  6. nickname: string;
  7. }
  8. interface State {
  9. pokemon: Pokemon[];
  10. }
  11. class PokemonIndex extends React.Component<void, State> {
  12. public state = {
  13. pokemon: [],
  14. };
  15. public async componentDidMount(): Promise<void> {
  16. try {
  17. const data = await Pokemon.index();
  18. this.setState({pokemon: data.data});
  19. } catch (err) {
  20. // eslint-disable-next-line no-console
  21. console.log(JSON.stringify(err, null, 2));
  22. }
  23. }
  24. public render(): JSX.Element {
  25. const pokemon: JSX.Element[] = this.state.pokemon.map(
  26. (pkmn): JSX.Element => (
  27. <li key={pkmn.id}>
  28. <Link to={`/pokemon/${pkmn.id}`}>{pkmn.nickname}</Link>
  29. </li>
  30. )
  31. );
  32. return <ul>{pokemon}</ul>;
  33. }
  34. }
  35. export default PokemonIndex;