javascript - 如何在 React-native 中显示来自 Firebase 存储的图像?

标签 javascript firebase async-await react-native firebase-storage

我正在尝试在我的 React-native 应用程序中显示来自 Firebase 存储的图像。我通过此 async 函数获取下载 URL:

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

我的问题是如何将 URL 传递给图像组件。我试过:

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

但它只返回对象。

我还找到了 getImage().then(result => console.log(result)) - 但我不知道如何在我的案例中使用它。

最佳答案

这可能不是更清洁的解决方案,因为我仍在学习 React Native 和 Firebase 产品,但是,这是我所做的:

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

@react-native-firebasedoc

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}} />
);

关于javascript - 如何在 React-native 中显示来自 Firebase 存储的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62943035/

相关文章:

javascript - 在 Vue 应用程序模板中,使用 ASYNC/AWAIT(Nativescript Playground)显示从 API 获取的数据

javascript - 无法在聚焦屏幕上重新渲染功能

javascript - 在 BlackBerry WebWorks 应用程序中显示 Google map

javascript - 获取完整的 html 而不仅仅是 innerhtml

firebase - “task.future.then ”不断在我的Flutter代码中引发错误

ios - 从 firebase Swift 加载聊天

javascript - 如何从 javascript 选择 div 中的文本(用于复制到剪贴板)?

firebase - 使用FirebaseUI将Firestore存储到recyclerview中,没有显示数据

javascript - Vue 导出导入异步等待停止

c# - 我怎样才能等待一系列任务并停止等待第一个异常?