2

I've got a Firebase app setup, with an array of items in the Realtime Database. Each item has an imagePath node, containing a Firebase Storage URI (eg. gs://bucket/images/stars.jpg).

The array of items is used in a <ListView/>, and I need to load an image in each row.

Seen as the images aren't stored directly in a "plain" URL format, I need to use the storage API to get that.

So in my renderRow(responseItem) function, I've got the following:

<View style={styles.imgWrap}>
    {this.getImage(responseItem.imgPath)}
</View>

And that function is the following:

getImage(path) {
    FirebaseApp.storage().refFromURL(path).getDownloadURL().then((url) => {

        return (
            <Image
                style={styles.candidateImg}
                source={{uri: url}}/>
        )

    })

}

The problem is, nothing's being returned.

If I run a console.log(url) above the return in getImage, it logs the correct URL, that I can open in browser.

Tldr; storage API is getting image URL correctly, but react-native isn't returning the <Image/> component.

1 Answer 1

0

I think the problem is in async invoking of getImage(), try this:

<View style={styles.imgWrap}>
    <Image
        style={styles.candidateImg}
        source={this.state.img}/>
</View>


getImage(path) {
    FirebaseApp.storage().refFromURL(path).getDownloadURL().then((url) => {
        this.setState({img: {uri: url}});
    })
}

// and invoke in renderRow
this.getImage(responseItem.imgPath)

Of course you should create an array of uri's to handle <ListView />

Sign up to request clarification or add additional context in comments.

1 Comment

Aha, so I'd do this.state.img as an array, adding each rows image to it, and then get the necessary array item in each row? With something like source={this.state.img[rowId]}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.