React Native-提取调用已缓存 [英] React Native - Fetch call cached
本文介绍了React Native-提取调用已缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用react native构建一个应用程序,该应用程序将依赖于来自服务器的最新信息进行提取调用。我注意到它似乎缓存了响应,如果再次运行该访存调用,它将返回缓存的响应,而不是服务器中的新信息。
I am building an app in react native which makes fetch calls that rely on the most up to date information from the server. I have noticed that it seems to cache the response and if i run that fetch call again it returns the cached response rather than the new information from my server.
我的函数是如下:
goToAll() {
AsyncStorage.getItem('FBId')
.then((value) => {
api.loadCurrentUser(value)
.then((res) => {
api.loadContent(res['RegisteredUser']['id'])
.then((res2) => {
console.log(res2);
this.props.navigator.push({
component: ContentList,
title: 'All',
passProps: {
content: res2,
user: res['RegisteredUser']['id']
}
})
});
});
})
.catch((error) => {console.log(error);})
.done();
}
和来自api.js即时通讯的函数如下:
and the function from api.js im calling is as follows:
loadContent(userid){
let url = `http://####.com/api/loadContent?User_id=${userid}`;
return fetch(url).then((response) => response.json());
}
推荐答案
您可以设置 Header
可以防止请求被缓存。
下面的示例:
You can set a Header
to prevent the request from being cached.
Example below:
return fetch(url, {
headers: {
'Cache-Control': 'no-cache'
}
}).then(function (res) {
return res.json();
}).catch(function(error) {
console.warn('Request Failed: ', error);
});
这篇关于React Native-提取调用已缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文