Closed devYuraKim closed 3 months ago
My code was lagging in state updates. Additionally, the values (0.05, 0.1 and 0.2) were too small and were just being displayed as 0. With larger numbers, the states were being updated even with my original code(but of course there was still the lag problem, so the calculation wasn't correct).
function handleChange1(e) {
const newTip1 = Number(e.target.value);
setTip1(newTip1);
setTotalTip((newTip1 + tip2) / 2);
}
function handleChange2(e) {
const newTip2 = Number(e.target.value);
setTip2(newTip2);
setTotalTip((newTip2 + tip1) / 2);
}
function TipSeparate({ onChange, children }) {
return (
<div>
{children}
<select onChange={onChange}>
<option value={0}>0%</option>
<option value={5}>5%</option>
<option value={10}>10%</option>
<option value={20}>20%</option>
</select>
</div>
);
}
States for
tip1
,tip2
are updated without any problem buttotalTip
won't change. Can't figure out what's missing betweenApp
andTip
component.