如果我写
function Component() {
const [isLoading, setLoading] = useState(true);
const request = () => {
setLoading(true)
console.log(isLoading)
setLoading(false)
console.log(isLoading)
}
}
两次都会注销“true”。为什么控制台中的状态没有更新?尽管它在 DOM 中运行良好。
this.setState({ ...}
将显示新的状态值
使用以下命令在状态中设置新值setState
or useState
hook 是一个异步进程。
如果您想在更改后记录新值,则必须将其与useEffect
hook
useEffect(() => {
console.log(isLoading)
}, [isLoading]);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)