React Native app won't fetch api data

2019-08-25 07:28发布

问题:

My React Native app won't fetch or get api data. It will not console log data or even an error. I've tried using fetch and axios.

fetch method:

    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 method:

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

I'm not sure what the problem is and I can't find a solution online.

回答1:

Please do check spelling function componentDidMount And try below sample code I have.

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

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

Hope it will help.