【问题标题】:Why am I getting the previous value of state?为什么我得到以前的状态值?
【发布时间】:2021-04-02 12:18:37
【问题描述】:

我正在尝试在事件处理程序中调用 onChange 中的值进行服务调用。但是在我的事件处理程序中,要么我得到了之前的状态值,要么我的状态没有得到更新。

这可能是什么原因造成的?

如何处理这种状态变化,以便它为我提供更新的值?

这是我的组件

function CountryDataAra65(props){

const [countryOption, setCountry] = useState("Select a country");
const [countryData, setCountryData] = useState([]);

var cardBody;

// Tags for table
const PPTag = "Purchasing Power";
const CLTag = "Cost of Living";
const HCTag = "Health Care";
const CRTag = "Crime Index";
const PLTag = "Pollution Index";
const QLTag = "Qualit of Life";

// When a country is selected
// When this event handler is called onChange of select, the state(countryOption) gives me the previous value
async function onCountrySelected(e){
    const val = e.target.value;
    await setCountry(val);

    console.log(countryOption);
    
    // To pass country as object
    const country = {
        "country": countryOption
    }

    // To get country's data
    // This is a service call
    await getCountryData(country)
    .then(data =>{
        setCountryData(data);
        console.log(data);
    })
    .catch(error =>{
        console.log(error);
    })
}

下面是我的选择输入


<select className="form-control" aria-label="Default select example"  onChange={onCountrySelected}>
  <option selected>Select a country</option>
  {
    props.data.map((item, key) => {
      return (
        <option defaultValue={item} key={key}>{item}</option>
      )        
    })
  }
</select>

【问题讨论】:

  • 您能否具体说明您从哪里获得旧值?

标签: reactjs express react-hooks react-state


【解决方案1】:

React 钩子(开箱即用)不是异步的。所以调用await setCountry 不会做任何事情。其次,如果你想在onCountrySelected方法中更新countryOption的值,你可以简单地使用e.target.value中的值。

function CountryDataAra65(props) {
  const [countryOption, setCountry] = useState("Select a country");
  const [countryData, setCountryData] = useState([]);

  var cardBody;

  // Tags for table
  const PPTag = "Purchasing Power";
  const CLTag = "Cost of Living";
  const HCTag = "Health Care";
  const CRTag = "Crime Index";
  const PLTag = "Pollution Index";
  const QLTag = "Qualit of Life";

  // When a country is selected
  // When this event handler is called onChange of select, the state(countryOption) gives me the previous value
  async function onCountrySelected(e) {
    const val = e.target.value;
    setCountry(val);

    console.log(countryOption);

    // To pass country as object
    const country = {
      country: val,
    };

    // To get country's data
    // This is a service call
    await getCountryData(country)
      .then((data) => {
        setCountryData(data);
        console.log(data);
      })
      .catch((error) => {
        console.log(error);
      });
  }
}

【讨论】:

  • 感谢@Dharman,我做到了,它有效,它解决了我的问题,但为什么状态没有立即更新
  • this 文章很好地解释了为什么 setState 不立即发生的确切原因。
  • 事情是在更新 countryOption 状态之后。我还尝试在其中使用 countryOption state 执行另一种方法,它也给了我之前的 state 值。为什么会这样?
猜你喜欢
  • 2019-12-16
  • 2021-03-07
  • 2023-03-17
  • 2020-01-28
  • 2013-09-09
  • 2012-04-03
  • 2015-10-20
  • 2022-11-15
  • 2021-03-28
相关资源
最近更新 更多