1

I've installed axios by this command:

npm i axios

I wrote the code below and everything I think is correct but React-Native doesn't show any data and throw any errors:

import React, {useState, useEffect} from 'react';
import axios from 'axios';

import {
  View,
  Text,
  StyleSheet
} from 'react-native';

const App = () => {
    
    const [data, setData] = useState([]);
    
    useEffect(() => {
        axios.get('http://localhost:8000/posts').then(res => {
            setData(res.data);
        });
    }, []);
    
    return (
        <View style={styles.mainBody}>
            {
                
                data.map(item => (
                    <View>
                        <Text>{item.fields.title}</Text>
                    </View>
                ))
                
            }
        </View>
    );
};
  • I've tested functional and class-based components for this.

  • I fetch data with Postman and works correctly.

  • I added .catch and it shows this: Error: Network Error

But it doesn't work on React-Native and because of no errors, I can't understand what's my problem. Any help will be appreciated.

  • Is it because of [this](https://stackoverflow.com/questions/51363339/react-native-app-transport-security-has-blocked)? – Adam Jenkins Aug 13 '21 at 00:33
  • @Adam I don't have any error. –  Aug 13 '21 at 00:36
  • A lack of data is an error. Please provide details - does the fetch complete? Does it even fire in the first place? Does the fetch hang indefinitely? Does it eventually throw (if you added a catch)? – Adam Jenkins Aug 13 '21 at 00:41
  • @Adam How should I check it in React-Native that fetch is fired in the first place? –  Aug 13 '21 at 00:47
  • Maybe chain a .catch to catch the error and try adding debugger before the call and follow the trace. – Ray Aug 13 '21 at 00:51
  • @Ray I added `.catch` and it shows this: `Error: Network Error` –  Aug 13 '21 at 00:55

1 Answers1

0

It's because you're using localhost in your URL, with react-native we cant use localhost but instead, we use the IPv4 address of our computer. So click on your wifi icon then click on the properties then look for IPv4 address something like: 193.93.443

So if to say your IPv4 address is 193.93.443, the local URL will be like this: http://193.93.443:8000/posts

Shamxeed
  • 382
  • 4
  • 8
  • I found the `IPv4` address by `ipconfig` command in CMD. I added as you said but nothing changed. –  Aug 13 '21 at 01:24
  • Your phone and your computer needs to be in the same WiFi connection. – Shamxeed Aug 13 '21 at 01:33
  • It's an emulator. They're same. I'm using Django for API. Nothing to do with Django? –  Aug 13 '21 at 15:29
  • Your can test your back-end with something like postman to be sure of whether it's working as expected. So as far as it's working smoothly on Postman then surely the issue is with your front-end. And that of emulator, I actually have never worked with it, I test my application on physical device. But be sure that you're not behind any proxy or using VPN. – Shamxeed Aug 13 '21 at 16:32