I have a react and firebase app wherein after successful login signup I need to display the username and the profile picture of the user.Its working until I refresh the page.After refreshing the username turns null and the profile picture is reduced to default profile Picture.Here’s the code for the same:
const Homepage = ({user,username,setusername}) => {
const [photourl,setPhotourl]=useState(profile)
const [photo,setPhoto]=useState(null);
const handleImageChange=(e)=>{
if(e.target.files[0]);
setPhoto(e.target.files[0]);
}
function handleImageClick(e){
if(photo)
{
const photoRef=ref(storage,photourl);
console.log(photoRef);
uploadBytes(photoRef,photo).then(()=>{
getDownloadURL(photoRef).then((url)=>{
setPhotourl(url);
}).catch((err)=>{
console.log(err);
})
}).catch((err)=>{
console.log(err);
});
alert("Profile Image has been changed successfully. Click OK and Wait for a second to view the changes.");
}
else
alert("Please upload a file")
}
// useEffect(()=>{
// if(user && user.photoURL)
// setphotourl(user.photoURL)
// })
return (
<>
<header>
<nav>
<div className="logo"><h1 className=""><a href="#">SOLRUF</a></h1></div>
<div className="menu">
<h3>Welcome {username}</h3>
<img className="avatar" src={photourl}/>
<button >Change Username</button>
</div>
</nav>
<main>
<section>
<h3>Welcome To India</h3>
<h1>Do come and Visit<span className="change_content"> </span></h1>
<p>"India once is not enough"</p>
<input type="file" onChange={(e)=>{handleImageChange(e)}}/>
<button onClick={(e)=>{handleImageClick(e)}}>Change Profile Pic</button>
</section>
</main>
</header>
</>
)
}
export default Homepage