2

I'm learning and doing a project on react-native. I am having problems in trying to render images from the local server. I figured out the way to render images, the problem I am facing is that the image from the local server does not render in my app. The link I added to the uri in source shows image in browser but does not render it in the app. I don't know where I am going wrong

I've tried checking if the link works or not, I used placeholder images to check if Image was working properly (it is).

 <Image source={{uri: 'http://10.0.2.2:8000/packageImages/asd1.jpg'}}
                       style={{width: 400, height: 400}} />

I expected the image to render but it does not render, nor does it throw any error.

2 Answers2

1

If you do truly want the IP assigned to your emulator:

adb shell
ifconfig eth0

Which will give you something like:

eth0: ip 10.0.2.15 mask 255.255.255.0 flags [up broadcast running multicast]

How to get the Android Emulator's IP address?

Faizan Shah
  • 123
  • 10
0

Sometimes I couldn't render images without SSL encription, can you try using some image from internet with one? You can also try this approach to check if there is some kind of error in your image:

class CustomImage extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            source: this.props.source
        }
    }



    shouldComponentUpdate(prevProps, prevState) {
        if (this.state.source !== prevState.source) return true

        return false
    }

    handleImageError = (error) => {

        if (this.props.semImagem) {
            this.setState({
                source: this.props.semImagem
            })
        } else {
            this.setState({
                source: resources.images.livroSemCapa
            })
        }

    }

    render() {

        let theSource = this.state.source

        if (this.state.source ?.uri === null || this.state.source ?.uri ?.trim() === "") {

            theSource = this.props.semImagem ? this.props.semImagem : resources.images.livroSemCapa
        }


        return (
            <Image
                onError={({ nativeEvent: { error } }) => this.handleImageError(error)}
                source={theSource}
                style={this.props.style}
                resizeMode={this.props.resizeMode || 'contain'}
            />
        )
    }
}

André Pinto
  • 104
  • 5