Home > OS >  Axios Network Error in React Native Android
Axios Network Error in React Native Android

Time:09-16

I Am currently learning react native and encountered an error "[AxiosError: Network Error]" while using "axios.get". I have tried several methods from stackoverflow and google but it didnt work.

Here Is My Code:

  const getData = async () => {
    await axios
      .get('https://www.reactnative.dev/movies.json')
      .then(({data}) => {
        setData(data.movies);
      })
      .catch(res => {
        console.warn(res);
      });
  };

  useEffect(() => {
    getData();
    //eslint-disable-next-line
  }, []);

CodePudding user response:

Not sure if this is your problem but it looks like you are mixing aysnc/await with a regular promise. Maybe try something like this:

const getData = async () => {
  try {
    const { data } = await axios.get('https://www.reactnative.dev/movies.json');
    setData(data.movies);
  } catch (error) {
    console.warn(error);
  }
};

useEffect(() => {
  getData();
  //eslint-disable-next-line
}, []);

CodePudding user response:

use async/await or regular promise it may not fix the issue but it's good practice

and make sure you have internet connection on emulator/device

CodePudding user response:

Issue has been resolved. There was an error from my network

  • Related