Fetching pokeapi data

Once again we’re going to jump headlong into the abyss into an empty Javascript file, so if you have the project from the last lesson still open, just delete it all. You don’t need it, we’re going to start over again and reinforce some of the things you’ve already learned. For right now, I want to set up some data that comes from an api. To do that I am going to use this site, “Pokeapi”. It’s at Pokeapi.org. They have a really cool api that’s open and lets you play with some fun data. So let’s just grab this url, and we use fetch to get that data. Now I’m just going to assume that works and we don’t have any problems and just handle the working case, in which case we’ll have a response and we’ll call .json on it. Just to verify that we actually have that request working properly, let’s take the resulting data and console.log it. Right now, we are not getting anything, so I’m going to open my console to see what’s up. We’re getting blocked because we’re using HTTP, and we’re mixing that with our sandbox here, which is HTTPS. We can fix that by changing that to HTTPS. We are in business, so close that console, we’re getting that there. Now what I want to make is something that actually shows a pokemon, so I’m going to use the api for getting a specific pokemon, and because I’m a nerd I know that the first one is bulbasaur. This is great, we’re all the data that we need. Now, I want to make this a callable function, so I’m going to take that out, we don’t want a console.log anymore now that we know that it is working, and I’m going to wrap it in a function. [Example]. What we’ll do is we’ll take an id as an argument, and we’ll change this up just a little bit. We’re going to us Javascript template strings and interpolate the id that we get. Now we can call this function in our apps, call it with an id, get that pokemon as .json data.

Play video: Fetching pokeapi data
  1. Course overview
  2. Project setup
  3. ReactDOM.render and React.createElement
  4. JSX
  5. Functional components
  6. Props
  7. Class Components
  8. onClick and other events
  9. Component initial state
  10. setState - object form
  11. setState - functional form
  12. Conditional rendering in JSX
  13. essential react 13
  14. 👉Fetching pokeapi data
  15. componentDidMount
  16. setState and fetch
  17. Lists with Array.map()
  18. componentWillReceiveProps
  19. Component props
  20. Render props
  21. Separation of Concerns with components
  22. Higher-order components
  23. defaultProps
  24. Render prop actions
  25. Course summary