我是 React Native 的新手,几周前才开始学习它。我正在尝试创建用户配置文件页面,从 Firestore 获取数据后,我无法将数据设置为userProfile
状态。
const {userProfile, setUserProfile} = useState(null);
const getUserProfile = async () => {
await firestore().collection("Users")
.doc(user.uid)
.get()
.then(documentSnapshot => {
if (documentSnapshot.exists) {
console.log('User data from Firestore: ', documentSnapshot.data());
setUserProfile(documentSnapshot.data());
}
});
}
useEffect (() => {
if (userProfile == null) {
getUserProfile();
console.log("User Data in userProfile state: ", userProfile);
}
}, [userProfile])
我收到了这个错误:
控制台日志显示userProfile
状态未定义,但是,我能够从 Firestore 检索数据而没有任何问题,如控制台日志中所示。
我已经尝试解决它很长时间了,但仍然无法解决它。任何帮助将不胜感激!
const {userProfile, setUserProfile} = useState(null); <----- error is here
const getUserProfile = async () => {
await firestore().collection("Users")
.doc(user.uid)
.get()
.then(documentSnapshot => {
if (documentSnapshot.exists) {
console.log('User data from Firestore: ', documentSnapshot.data());
setUserProfile(documentSnapshot.data());
}
});
}
useEffect (() => {
if (userProfile == null) {
getUserProfile();
console.log("User Data in userProfile state: ", userProfile);
}
}, [userProfile])
而不是const {userProfile, setUserProfile} = useState(null);
将其替换为const [userProfile, setUserProfile] = useState(null);
因为你在curly brackets
使用{userProfile,setUserProfile}
你需要用square brackets
[userProfile,setUserProfile]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句