react使用hooks踩坑记录
1、useState异步回调的问题
当使用usestate对数据进行更新,并不能立刻获取到最新的数据。
const [a, setA] = useState(1);
const handleTest = () => {
console.log(a) // 1
setName(2)
console.log(a) // 1
}
解决方案1:配合useEffect使用
const [a, setA] = useState(1);
const handleTest = () => {
原创
2021-11-11 15:04:32 ·
704 阅读 ·
0 评论