Jiaxi Lin
Jiaxi Lin

Reputation: 13

React Native Axios.get returned results not fresh

I'm using axios to fetch data for my React Native app and I'm having an issue only for iOS. I am able to fetch data from my server perfectly fine, however if I change any data from my API, the changes doesn't reflect in iOS at all, only when I re-install the app then the changes will take place. I'm still not able to pinpoint what is causing the issue. This is only happening in iOS, Android works perfectly fine.

Fetch Data code:

axios.get('http://www.example.com/api')
.then((response) => {
  // console.log(response);
  this.setState({ data: response.data, loading: false });
});

Please let me know if I miss out any information.

If this has already been asked, I would greatly appreciate if you are able to point me in the right direction.

Thank you so much!

Upvotes: 0

Views: 1115

Answers (2)

Chamod Pathirana
Chamod Pathirana

Reputation: 758

It can be resolved by adding headers: {'Cache-Control': 'no-cache'} to the header.

axios.get('http://www.example.com/api',
  headers: {'Cache-Control': 'no-cache'})
.then((response) => {
  // console.log(response);
  this.setState({ data: response.data, loading: false });
});

Upvotes: 2

shayuna
shayuna

Reputation: 484

my guess is that the page is read from the cache and so you get an old copy. what you need to do is to add a date stamp to the link, in order to force the app to load a 'fresh' page. it goes something like that:

 axios.get('http://www.example.com/api?dt='+(new Date()).getTime())
.then((response) => {
     // console.log(response);
     this.setState({ data: response.data, loading: false });
});  

Upvotes: 2

Related Questions