skip to Main Content

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
  }, []);

2

Answers


  1. 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
    }, []);
    
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search