亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 Firebase 下載圖像(React Native)

從 Firebase 下載圖像(React Native)

慕絲7291255 2023-09-07 17:03:53
我正在嘗試從 Firebase 存儲下載圖像并將其顯示在我的應用程序中。我嘗試獲取下載網址的方法如下:let imageRef = firebase.storage().ref('users/' + firebase.auth().currentUser.uid + '/' + 'userImage');    imageRef    .getDownloadURL()    .then((url) => {    console.log(url)    })    .catch((e) => console.log('getting downloadURL of image error => ', e));現在,這完美地注銷了 url。然而,從該函數外部無法訪問它。如果我嘗試在 .catch 之后立即訪問它,我會收到以下錯誤:ReferenceError:找不到變量:url當然,這意味著當我嘗試按以下方式顯示圖像時,我無法調用該網址:<Image source={{uri: url}} />關于如何處理和解決這個問題有什么想法嗎?多謝!
查看完整描述

1 回答

?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

解決方案是將 URL 存儲在組件的狀態中,而不是變量中:


let imageRef = firebase.storage().ref('users/' + firebase.auth().currentUser.uid + '/' + 'userImage');

? ? imageRef

? ? .getDownloadURL()

? ? .then((url) => {

? ? ? setState({ url: url });

? ? })

? ? .catch((e) => console.log('getting downloadURL of image error => ', e));

這不僅可以確保 render 方法可以找到它,而且可以在計算出 URL 后重新渲染輸出。

如果你使用 hooks,你會得到類似的東西:

const [url, setUrl] = useState();


let imageRef = firebase.storage().ref('users/' + firebase.auth().currentUser.uid + '/' + 'userImage');

? ? imageRef

? ? .getDownloadURL()

? ? .then((url) => {

? ? ? setUrl(url);

? ? })

? ? .catch((e) => console.log('getting downloadURL of image error => ', e));


查看完整回答
反對 回復 2023-09-07
  • 1 回答
  • 0 關注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號