Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component, Fragment } from 'react';
  2. import { NavBar } from './components/layout/navbar.component';
  3. import { SearchPokemon } from './components/pokemon/search-pokemon.component';
  4. import { PokemonList } from './components/pokemon/pokemon-list.component';
  5.  
  6. class App extends Component {
  7.   state = {
  8.     search: '',
  9.     pokeData: []
  10.   };
  11.  
  12.   async componentDidMount() {
  13.     try {
  14.       // Getting all of the pokemon
  15.       const res = await fetch(`https://pokeapi.co/api/v2/pokemon/?limit=964`);
  16.       const data = await res.json();
  17.       this.setState({ pokeData: data });
  18.     } catch (e) {
  19.       console.log(e);
  20.     }
  21.   }
  22.  
  23.   /*  async componentDidUpdate() {
  24.     try {
  25.       this.state.pokeData.map(async pokemon => {
  26.         const res = await fetch(
  27.           `https://pokeapi.co/api/v2/pokemon/${pokemon.name}?limit=1`
  28.         );
  29.         const data = await res.json();
  30.         console.log(data);
  31.         // this.setState({ pokeSprites: data.sprites.front_default });
  32.       });
  33.     } catch (e) {
  34.       console.log(e);
  35.     }
  36.   } */
  37.  
  38.   handleChangePokemon = async e => {
  39.     this.setState({ search: e.target.value });
  40.   };
  41.  
  42.   getPokemon = async pokemon => {
  43.     try {
  44.       const res = await fetch(`https://pokeapi.co/api/v2/pokemon/${pokemon}`);
  45.       //const res = await fetch(`https://pokeapi.co/api/v2/pokemon/pikachu`);
  46.       const data = await res.json();
  47.       this.setState({ pokeData: data });
  48.       //console.log(data);
  49.     } catch (e) {
  50.       console.log(e);
  51.     }
  52.   };
  53.  
  54.   render() {
  55.     const { pokeData } = this.state;
  56.  
  57.     return (
  58.       <Fragment>
  59.         <NavBar />
  60.         <div className='container py-5'>
  61.           <div className='row'>
  62.             <SearchPokemon
  63.               getPokemon={this.getPokemon}
  64.               handleChangePokemon={this.handleChangePokemon}
  65.               search={this.state.search}
  66.             />
  67.           </div>
  68.           <div className='row'>
  69.             <PokemonList pokeData={pokeData} />
  70.           </div>
  71.         </div>
  72.       </Fragment>
  73.     );
  74.   }
  75. }
  76.  
  77. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement