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));
添加回答
舉報