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

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

如何更改圖像源?

如何更改圖像源?

青春有我 2023-12-14 16:39:52
我正在嘗試更改圖像并將其用作 firebase 中的用戶個人資料照片我可以將圖像上傳到存儲,但無法使用下面的代碼獲取它們的網址storage .ref("images") .child(image.name) .getDownloadURL() .then(url => {  setUrl(url)})然后我想使用該 url 來更改圖像 srcconst upUserPhoto = () => {user.updateProfile({photoURL: url})} <img src={user.photoURL}/>但它不起作用..我該如何解決這個問題,有更好的方法嗎?請賜教。完整代碼import React, { useState } from 'react'import fire from "./fire"function Profilephoto({ open, children, onClose }) {    const[image, setImage] = useState(null)    const[url, setUrl] = useState("")    const[progress, setProgress] = useState(0)    const storage = fire.storage();    const user = fire.auth().currentUser    if (!open) return null        const asdChange = (e) => {        if(e.target.files[0]) {            setImage(e.target.files[0])        }    }    const asdUpload = () => {        const uploadTask = storage.ref(`ìmages/${image.name}`).put(image)        uploadTask.on(            "state_changed",            snapshot => {                const progress= Math.round(                    (snapshot.bytesTransferred/snapshot.totalBytes) * 100                )                setProgress(progress)            },            error => {                console.log("error")            },            () => {                storage                    .ref("images")                    .child(image.name)                    .getDownloadURL()                    .then(url => {                        setUrl(url)                    })            }        )     }    const setProfile = () => {        user.updateProfile({photoURL: url})    }            return (        <>        <div className="modalb"></div>        <div className="modala">                        <progress value={progress} max="100"/>                        <input type="file" onChange={asdChange}/>            <button onClick={asdUpload}>Upload</button>        </div>        </>    )}export default Profilephoto
查看完整描述

1 回答

?
叮當貓咪

TA貢獻1776條經驗 獲得超12個贊

做類似的事情,聽你的 setUrl 就像useEffect

useEffect({
    setProfile()
}, [url]);


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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