我正在react native中构建一个应用程序,它可以根据服务器上的最新信息进行获取调用.我注意到它似乎在缓存响应,如果我再次运行该fetch调用,它将返回缓存的响应,而不是来自服务器的新信息.

我的职责如下:

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 im呼叫如下:

loadContent(userid){
  let url = `http://####.com/api/loadContent?User_id=${userid}`;
  return fetch(url).then((response) => response.json());
}

推荐答案

您可以设置Header以防止请求被缓存.

return fetch(url, {
  headers: {
    'Cache-Control': 'no-cache'
  }
}).then(function (res) {
  return res.json();
}).catch(function(error) {
  console.warn('Request Failed: ', error);
});

Reactjs相关问答推荐

如何将google地址lat收件箱设置为输入值?

为什么我无法访问窗口事件处理程序中的状态?

防止在Reaction中卸载

不同步渲染

对搜索引擎优化来说,react 头盔的异步足够了吗?

带有样式的工具提示导致无法向功能组件提供参考警告

如何动态地改变<; Select >;React和Tailwind元素?

无法在 NextJS 中获取嵌套对象

如何使用服务器中的数据自动填充表单字段?

try 从 React JS 构建此教程游戏但无法继续前进

React 应用程序可以嵌入到 PowerPoint 中吗?

单击按钮时如何添加或删除 3d 对象

React - 添加了输入数据但不显示

antd 找不到 comments

map 未显示在react 传单中

Cypress - 替换 React 项目中的变量

React Router v6 - 访问嵌套路由和处理手写 url 的正确方法

React Router v6 - 访问嵌套路由和处理手写 url 的正确方法

网格项不缩小以适应包含的可滚动列表

在 redux 状态更改时更新react 按钮禁用状态