从您的评论来看,您的代码结构如下:
const [quantityKm, setQuantityX] = useState({ x: 0 });
const handleKmValue = ...
const calculatePrice = ...
const handleChange = event => {
handleKmValue(event);
calculatePrice();
}
<Select onChange={handleChange} />
相反,使用:
const [quantityKm, setQuantityX] = useState({ x: 0 });
useEffect(() => {
// move the calculatePrice functionality into the effect
console.log(quantityKm.x);
setValueMonth((parseFloat(quantityKm.x.toFixed(2)) * valueOfKm))
}, [quantityKm.x]) // run effect if value's changed
<Select onChange={handleKmValue} />