Giter Club home page Giter Club logo

iss_tracker's Introduction

React Data Fetching: ISS Tracker

This challenge asks you to refactor the existing code of the ISS Tracker to use SWR. You will need to fetch on interval and refetch on click of a button.

Task

  • Start the development server and make yourself familiar with the application.
  • Switch to the ISSTracker component: it features a coords state, a getISSCoords function and a useEffect to fetch data on interval.

Fetching on load

  • Comment out all unnecessary code:

    • the coords state,
    • the getISSCoords function,
    • the useEffect,
    • for now: the onRefresh prop passed to the Controls component (you will adapt this in a minute).
  • Implement fetching of the same data with SWR.

    • SWR is already installed, so you just have to import it.
    • Handle the isLoading and error states provided by SWR as well.
    • Make sure data is defined before working with it.
    • Remember to adapt the fetcher function to create a proper error state as explained in the SWR docs.
    • Note: SWR needs you to destructure a data object, but the Map and Controls component need longitude and latitude as separate props; how can you simply pass the coordinates from data without changing the Map/index.js and Controls/index.js files? (Hint: there are several ways to do this!)

โœจ You should now see the exact position of the ISS again when refreshing the page!

Implement interval

  • Refetch the data of the ISS on an interval of 5 seconds. See the SWR docs to find help how to do it.

Refetch on button click

The only thing not working yet is the "Refresh" button because the getISSCords function does not exist anymore.

  • Destructure the mutate function provided by the useSWR hook.
  • Pass it to the onRefresh prop of the Controls component. You can use an inline function as in onReload={() => handleReload()}.

โœจ Congratulations, you can now see the exact position of the ISS whenever you want with the help of SWR!

Notes

Development

CodeSandbox

Select the "Browser" tab to view this project. If this project contains tests, select the "Tests" tab to check your progress.

๐Ÿ’ก Please note that Next.js support on CodeSandbox is not great.

Local development

To run project commands locally, you need to install the dependencies using npm i first.

You can then use the following commands:

  • npm run dev to start the development server
  • npm run build to create a production build
  • npm run start to start the production build
  • npm run test to run the tests in watch mode (if available)

๐Ÿ’ก This project requires a bundler. You can use npm run dev to start the development server. You can then view the project in the browser at http://localhost:3000. The Live Preview Extension for Visual Studio Code will not work for this project.

iss_tracker's People

Contributors

hannesoster avatar

Watchers

 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.