Search code examples
react-nativefetchlocal-storage

Constraining API Call / Fetch to once per day / usage - React Native


I have searched everywhere and just cannot find an answer for this. What I want to do can be described as follows:

On a user's first time opening the application for a given day, make an API call. Store the API call in local storage, and then for the rest of the day, simply rely on the stored data in local storage (data comes from local storage as opposed to API call now).

However, once that full day passes, a new API call is made the next time the user opens the application and that data is used to update the data in local storage. Rinse and repeat. Is there a way to do this?


Solution

  • You can store the date of when the last API call was made, for example, lastAPIFetchDate, then each time the app opens you compare lastAPIFetchDate with today's date and if the lastAPIFetchDate is before today's date, make the API call, if not, get the data from the storage.