【发布时间】:2021-06-01 07:11:57
【问题描述】:
我创建了一个这样的函数。
export function Counter() {
const [count, setCount] = useState(0);
const countUp = () => {
setCount(count + 1);
}
const countUpAndShow = () => {
setCount(count + 1);
alert(count);
}
// I won't call after countUp function, call only countUpAndShow function.
useEffect(() => {
alert(count);
},[count])
return <div>
<button onClick={countUp}>count up!</button>
<button onClick={countUpAndShow}>show count!</button>
</div>
}
我想在setCount() 之后拨打alert(count)。
但alert(count) 未正确显示计数。
然后,我像上面一样使用useEffect。但我只想调用alert() countUpAndShow 函数。
如何解决?
【问题讨论】:
标签: reactjs react-hooks