【发布时间】:2021-07-18 12:00:29
【问题描述】:
所以,我要做的是向按钮添加一个事件侦听器,当按下该按钮时,它会获取状态的值并在控制台记录它。但即使在多次 setState 调用之后,记录的值也不会更新。
useEffect(() => {
const seamCarve = document.getElementById("process");
seamCarve.addEventListener("click", (e) => {
console.log(seamValue);
});
}, []);
然后是触发它的按钮
<div id="seamvalue">
<Typography variant="h6" align="center">
Seams Reduction:<span id="valueOfSeam"> {seamValue} </span>
</Typography>
</div>
<Button
id="leftslider"
variant="contained"
color="primary"
onClick={() =>
seamValue - 10 > 0 ? setSeamValue(seamValue - 10) : setSeamValue(0)
}
>
<Typography>{"<"}</Typography>
</Button>
<Button
id="rightslider"
variant="contained"
color="primary"
onClick={() => setSeamValue(seamValue + 10)}
>
<Typography>{">"}</Typography>
</Button>
<Button id="process" variant="contained" color="primary">
<Typography>Carve</Typography>
</Button>
当我单击滑块时,值会发生变化,但是当我按下带有关联事件侦听器的 id="process" 按钮时,即使在更新状态后它也只会打印 20。
【问题讨论】:
标签: javascript reactjs react-state-management