我对反应很陌生,我试图从 firebase 数据库加载个人资料图片。即使该属性不为 null,它仍然会显示 TypeError: Cannot read property 'pfpUrl' of null。
import React from "react"
import { NavLink } from "react-router-dom";
import { useState, useEffect } from "react"
import firebase from "firebase";
import fire from "./firebase"
import { useHistory } from "react"
function Header(){
const [pfp, setPfp] = useState(null)
const [userID, setUserID] = useState(null);
firebase.auth().onAuthStateChanged((user)=>{
if(user){
setUserID(user.uid)
firebase.database().ref("users/"+userID+"/public/profile/pic").on("value", (snapshot)=>{
setPfp(snapshot.val().pfpUrl)
})
}else{
}
})
function signOut(){
firebase.auth().signOut().then(()=>{
console.log("successfully signout")
})
}
return(
<header>
<div id = "hd-container">
<div id = "logo-contain">
<NavLink to = "/"><h1>Logo</h1></NavLink>
</div>
<div id ="search">
<div className = "bar">
<input id = "searchBar" placeholder = "search"></input>
</div>
<div className = "searchBtn">
<button id = "srhBtn">Search</button>
</div>
</div>
即使加载了个人资料图片,错误仍然出现,有解决办法吗?
喵喵时光机
开心每一天1111
相关分类