0

I am new in react-native and I m trying to get Image url from JSON API.When i run my code i got an error "source is undefined".So i just want to know that how can i get my Image url from JSON API.I am able to fetch the response of URL please check my code .

    async componentDidMount() {
    const DEMO_TOKEN = await AsyncStorage.getItem('isLoggedIn');
        const url11 = 'http://104.197.28.169:3000/userProfile';
        fetch(url11,{
            method: 'GET',
            headers: {
                Authorization: 'Bearer ' + DEMO_TOKEN,
            },
            //Request Type
        })
            .then(response => response.json())
            .then(responseJson => {
                console.log('UserProresponse', responseJson)
                this.setState({
                    dataSource : responseJson,
                    isLoading: false,
                });
            })
            .catch(error => console.log(error));
    }

  <View style={styles.imageView}>
                            <Image style={styles.logoImg}
                                source={require(this.state.dataSource.data.photograph)}>
                              </Image>
   </ VIew>

I am getting this console.log response.Here I am able to get the "firstName" , but I am unable to get the "photograph" from this response.Please check.

 {
 "message": "user profile",
"data": {
    "id": 1,       
    "firstName": "Mohd",
    "lastName": "Akram",
    "dateOfBirth": "1995-08-25",
    "gender": "male",
    "maritalStatus": "unmarried",       
    "photograph": "images/2020-04-28T10-37-21.149Zdutch.png",        
    "userId": 2,
 },
"status": 1
}
Rohit
  • 23
  • 5

1 Answers1

0

You should use uri: if get from internet or server.

example :

<Image source={{uri: this.state.dataSource.data.photograph}} style={{width:200, height: 200}} />

And don't forget to define the size (width and height) the image, if you get Image from rest API in React Native

zidniryi
  • 1,212
  • 3
  • 15
  • 36