[英]React hooks useState hook
The doubt is, in the webpage the value gets incremented and displays but in console it displays the previous value......what am i not understanding?问题是,在网页中,值会增加并显示,但在控制台中会显示以前的值......我不明白什么?
when i click on the button, in the webpage the value gets incremented but whereas at the console output it's still the previous value:/当我单击按钮时,在网页中的值会增加,但在控制台 output 它仍然是以前的值:/
function App() {
function handleClick()
{
setValue((prev)=>prev+1)
console.log(value)
}
const [value,setValue]=useState(0);
return (
<div className="App" style={{fontSize:"100px"}}>
{value}
<button onClick={handleClick}>increment</button>
</div>
);
}
export default App;
This is beacause setValuse
is async function, and value in state will be update in next renderer.这是因为
setValuse
是异步的 function,并且 state 中的值将在下一个渲染器中更新。
Check this:检查这个:
function handleClick()
{
setValue((prev)=>prev+1)
console.log(value)
}
const [value,setValue]=useState(0);
useEffect(() => { console.log(value)}, [value])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.