[英]How check state values(obj value) conditionally in react native
I need to check state value conditionally, I fetch user data from firestore as follow我需要有条件地检查 state 值,我从 firestore 获取用户数据如下
const[userdata,setUserdata]=useState()
const fetchCard = async()=>{
// user
const uRef = query(doc(db,'users',user.uid))
await getDoc(uRef)
.then((snapshot)=>{setUserdata(snapshot.data())})
.catch((error)=>{
alert(error)
})
console.log("here",userdata)
}
it get user data as objects它将用户数据作为对象
here Object {
"age": "25",
"displayName": "ghd",
"gender": "female",
"id": "E8R52y6dD8XI3shXhnhS9pVzUpe2",
"interestIn": "male",
"job": "gdh",
"photo": "https://firebasestorage.googleapis.com/v0/b/match-me-now.appspot.com/o/images%2FE8R52y6dD8XI3shXhnhS9pVzUpe2%2FprofilePicture.jpeg?alt=media&token=9084cdcb-beb7-44d3-8ab9-98e193955e75",
"timestamp": Object {
"nanoseconds": 755000000,
"seconds": 1639913879,
},
}
but I need check this values conditionally when this value has no value, to handle error.但是当这个值没有值时,我需要有条件地检查这个值,以处理错误。 because I will use this value in useEffect,
因为我会在useEffect中使用这个值,
useEffect(()=>{
fetchCard()
console.log("in effect",userdata.age)
},[])
if there is no object value in fetchCard, this will cause error in useEffect when call value from useEffect.如果 fetchCard 中没有 object 值,这将导致从 useEffect 调用 value 时 useEffect 出错。 So I want to check userdata value has or not.
所以我想检查用户数据值有没有。 if array value, just check with
const newdata = userdata.length > 0? userdata: ["no"]
如果是数组值,只需检查
const newdata = userdata.length > 0? userdata: ["no"]
const newdata = userdata.length > 0? userdata: ["no"]
but how can check this object values has or not?但是如何检查这个 object 值有没有? need help.
需要帮忙。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.