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.
- Start the development server and make yourself familiar with the application.
- Switch to the
ISSTrackercomponent: it features acoordsstate, agetISSCoordsfunction and auseEffectto fetch data on interval.
-
Comment out all unnecessary code:
- the
coordsstate, - the
getISSCoordsfunction, - the
useEffect, - for now: the
onRefreshprop passed to theControlscomponent (you will adapt this in a minute).
- the
-
Implement fetching of the same data with
SWR.SWRis already installed, so you just have to import it.- Handle the
isLoadinganderrorstates provided bySWRas well. - Make sure
datais defined before working with it. - Remember to adapt the
fetcherfunction to create a propererrorstate as explained in the SWR docs. - Note:
SWRneeds you to destructure adataobject, but theMapandControlscomponent needlongitudeandlatitudeas separate props; how can you simply pass the coordinates fromdatawithout changing theMap/index.jsandControls/index.jsfiles? (Hint: there are several ways to do this!)
✨ You should now see the exact position of the ISS again when refreshing the page!
- Refetch the data of the ISS on an interval of 5 seconds. See the SWR docs to find help how to do it.
The only thing not working yet is the "Refresh" button because the getISSCords function does not exist anymore.
- Destructure the
mutatefunction provided by theuseSWRhook. - Pass it to the
onRefreshprop of theControlscomponent. You can use an inline function as inonReload={() => handleReload()}.
✨ Congratulations, you can now see the exact position of the ISS whenever you want with the help of SWR!
- You only have to touch the
./components/ISSTracker/index.jsfile.
To work locally, please install the dependencies using npm i first.
Run npm run dev to start a development server and open the displayed URL in a browser.
Use npm run test to run the tests.
Select the "Preview: 3000" tab to view this project.
Select the "Tests: logs" tab to view the tests.
The
npm run devandnpm run testscripts run automatically.
You can use the following commands:
npm run devto start a development servernpm run buildto build the projectnpm run startto start a production servernpm run testto run the testsnpm run lintto run the linter