首页 > 解决方案 > 使用 useState 时状态更新延迟在选择列表内

问题描述

我宣布useState<ICurrency>

const [hwSalesPriceCurrency, setHwSalesPriceCurrency] = useState<ICurrency>(); 

有一个 html,其中我有一个选择列表作为

<label>
        Price Currency12:<span className="redStar">*</span>
</label>
<Select
        type="input-single"
        placeholder="Select Currency"
        disabled={false}            
        label={
          uscCurrencyEdit !== ""
            ? uscCurrencyEdit
            : hwSalesPriceCurrency
              ? hwSalesPriceCurrency.unit
              : ""
        }            
        items={currencies.map(v => ({ title: v.unit, value: v }))}            
        onChange={({ value }: { value: ICurrency }) => {              
          console.log("onChange value::");
          console.log(value);              
          setHwSalesPriceCurrency(value);
          uscCurrencyChanged(value.unit);              
          pricingTotal();
        }}
/>

当我选择不同的值时, onChange 会被触发。我看到 onChange 中的值是我选择的最新值,但setHwSalesPriceCurrency(value)没有将状态更新为最新值。它将其更新为以前的值。它旁边还有另一个文本框,它与此选择列表无关,但是在将一些数字输入文本框后,值会更新。

有没有什么方法可以让状态更新立即更新,而无需在另一个文本框中输入一些数字?

标签: reactjs

解决方案


推荐阅读