index.tsx 973 B

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