【问题标题】:React Native app won't fetch api dataReact Native 应用程序不会获取 api 数据
【发布时间】:2018-01-12 23:51:00
【问题描述】:

我的 React Native 应用程序不会获取或获取 api 数据。它不会控制台日志数据甚至错误。我试过使用 fetch 和 axios。

获取方法:

    export default class List extends React.Component {
      commponentDidMount() {
        fetch(URL)
        .then((response) => response.json())
        .then((responseData) => {
          console.log(responseData);
        })
        .catch(error => {
            console.log(error);
        });
      }
    }

axios 方法:

    export default class List extends React.Component {
      commponentDidMount() {
        axios.get(URL)
        .then(function (response) {
          console.log(response);
        })
        .catch(function (error) {
          console.log(error);
        });
      } 
    }

我不确定问题是什么,我在网上找不到解决方案。

【问题讨论】:

  • commponentDidMount() 中的拼写错误。是componentDidMount()

标签: react-native react-native-android axios fetch-api


【解决方案1】:

请检查拼写功能componentDidMount 并尝试下面的示例代码。

componentDidMount() {
  let URL = `https://...`;

  fetch(URL)
  .then(res => res.json())
  .then(res => {
      console.log('response:', res.data);
  });
}

希望它会有所帮助。

【讨论】:

  • 我得到可能的未处理承诺拒绝(id:0):当我将上述代码与 lorum ipsum api 一起使用时。
  • 你应该分享你的源代码,如果我们可以检查并尽可能地纠正它。
猜你喜欢
  • 2021-04-09
  • 2021-07-19
  • 2021-09-26
  • 2019-11-27
  • 1970-01-01
  • 2022-01-17
  • 2017-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多