Skip to content
Advertisement

how to reverse fetch images from nasa APOD api in react

** I fetched content from API with this code**

JavaScript

and output of this code is like this enter image description here and I want Button here with next and then that will show previous day images so can anyone tell me how to reverse that means after clicking on the next button by reversing the previous day images will show because NASA APOD(astronomy picture of the day )daily shown to all users like that is updated daily I know we can reverse that but can anyone tell me how to do that?

Advertisement

Answer

You can use date query parameter of apod api to get data for a specific date. This has default value today. Date needs to be in YYYY-MM-DD format. See apod section at https://api.nasa.gov/

If you want to request data for 2 January 2021 you’ll have to send request to this :

JavaScript

Note the date parameter

To get the previous day date use :

JavaScript

for formatting date in YYYY-MM-DD format see this question.

In onClick function of your button you’ll make this http request and then change state with setPhotoData function.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement