show.tsx 1.1 KB

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