show.tsx 947 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import React from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import {Link} from 'react-router-dom';
  4. import {Pokemon} from '../../../api';
  5. export interface IPokemon {
  6. id: number;
  7. nickname: string;
  8. }
  9. interface State {
  10. pokemon: IPokemon;
  11. }
  12. class PokemonShow extends React.Component<RouteComponentProps<any>, State> {
  13. state = {
  14. pokemon: {
  15. id: null,
  16. nickname: null,
  17. },
  18. };
  19. async componentDidMount() {
  20. try {
  21. const data = await Pokemon.get(this.props.match.params.id);
  22. this.setState({pokemon: data.data});
  23. } catch (err) {
  24. // eslint-disable-next-line no-console
  25. console.log(JSON.stringify(err, null, 2));
  26. }
  27. }
  28. render(): JSX.Element {
  29. return (
  30. <>
  31. {this.state.pokemon.id}: {this.state.pokemon.nickname}
  32. <p>
  33. <Link to={`${this.props.match.url}/404`}>404 page</Link>
  34. </p>
  35. </>
  36. );
  37. }
  38. }
  39. export default PokemonShow;