Code Monkey home page Code Monkey logo

fetch-in-react's Introduction

Fetch in React

Creative Commons License

This is part of Academy's technical curriculum for The Mark. All parts of that curriculum, including this project, are licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

We looked at fetching data from an API away from a browser environment, when we were running TS with ts-node (and, under the hood, running JS with node).

We'll now look at how fetch can be used to load external data from an API into React in a browser environment.

Learning Outcomes

  • Load data from an API into React with fetch and setState
  • Use conditional rendering in React

Demo: fetching a joke

๐ŸŽฏ Success criterion: You can run the React app which fetches a joke in

This repo has an example React app which loads data from a joke API when a button is clicked.

There's a short delay between the button being clicked and the joke being loaded - because it takes some time for the transportation of data:

  • HTTP Request: front-end asks for a joke from https://jokestemp.neillbogie.repl.co/jokes/general/random
  • HTTP Response: the joke API server responds with a random joke

The handleGetJoke function has been written in both async/await style and Promise .then style. (Check that both work by commenting and de-commenting.)

Exercise 1: showing a single random dog picture

๐ŸŽฏ Success criterion: Your React app loads in a new picture of a random dog of a fixed given breed every time the button is clicked

Now, let's change our joke fetching app to a dog picture fetching app.

We'll use this API.

Make the relevant changes to the React app so that you are fetching from this API (and rendering an image using the data received).

(It's up to you whether you use the async/await or .then!)

Exercise 2: accumulating images

๐ŸŽฏ Success criterion: Your React app contains a growing collection of dog pictures, adding a new random one every time a button is clicked

Currently, we're fetching a new dog picture every time we click a button - and discarding the previous one.

Instead, let's start to build up a nice, growing collection of them instead.

It may be helpful here to look up previous demos for array state!

fetch-in-react's People

Contributors

richardcrng avatar mariatens avatar nbogie avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.