最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to display Image from Firebase storage in React-native? - Stack Overflow

programmeradmin2浏览0评论

I am trying to display my image from Firebase storage in my React-native app. I get the download URL through this async function:

async function getImage() {
  const url = await storage()
  .ref('garnele.jpg')
  .getDownloadURL()
  console.log(url)
  return url
}

I am trying to display my image from Firebase storage in my React-native app. I get the download URL through this async function:

async function getImage() {
  const url = await storage()
  .ref('garnele.jpg')
  .getDownloadURL()
  console.log(url)
  return url
}

My problem is how to pass the URL to the Image ponent. I tried:

<Image
    source={{uri: getImage() }}
 />

but it only returns the object.

I also found getImage().then(result => console.log(result)) - but I don't know how to use it in my case.

Share Improve this question edited Jul 17, 2020 at 13:05 Adrian Mole 51.9k192 gold badges58 silver badges94 bronze badges asked Jul 16, 2020 at 20:32 MargokMargok 551 silver badge3 bronze badges 2
  • I don't have React experience, but related issues suggest storing the return value of the async function in the ponent's state: stackoverflow./questions/59070347/… stackoverflow./questions/55929760/… – bags Commented Jul 16, 2020 at 20:46
  • thanks a lot, that was the post i was Looking for! – Margok Commented Jul 16, 2020 at 21:32
Add a ment  | 

1 Answer 1

Reset to default 9

It is probably not the cleaner solution as I'm still learning react native and firebase products, but, here is what I did :

import storage from '@react-native-firebase/storage';

@react-native-firebase and doc

const [imageUrl, setImageUrl] = useState(undefined);

useEffect(() => {
    storage()
      .ref('/' + 'FA984B65-38D4-44B1-BF02-4E7EF089773B.jpg') //name in storage in firebase console
      .getDownloadURL()
      .then((url) => {
        setImageUrl(url);
      })
      .catch((e) => console.log('Errors while downloading => ', e));
  }, []);

return (
    <Image style={{height: 200, width: 200}} source={{uri: imageUrl}} />
);
发布评论

评论列表(0)

  1. 暂无评论