【发布时间】:2020-08-29 21:55:21
【问题描述】:
我有一个看起来像这样的 useEffect 函数。
useEffect(() => {
getColls()
console.log(pageColl)
}, [])
getColls() 函数设置 pageColl 变量。
//getColls()
const getColls = () => {
Service.getCollections()
.then(data =>
setPageColl(data.collections.find(coll => coll._id === userId))
)
}
//states
const [pageColl, setPageColl] = useState(null);
pageColl 应该设置为一个对象,但由于某种原因,当我尝试使用 console.log(pageColl) 时,我得到了一个 null。就好像 setPageColl 从未被调用过。我知道数据是在 getCols 中返回的,因为我可以在那里进行控制台记录。
【问题讨论】:
-
听起来
.find(coll => coll._id === userId)找不到匹配项?在调用setPageColl调试之前仔细检查结果
标签: reactjs react-hooks use-effect