简体   繁体   English

当设置状态为异步时如何立即将变量的值存储在状态中

[英]How to store the value of a variable in state immediately when set state is async

I am new to react and am trying to store the value in state for customer my code for getting the value of the dropdown is this:我刚开始反应,并试图为客户存储状态中的值,我用于获取下拉值的代码是这样的:

handleChangeCC = customercode => {
    this.setState({ customercode });

    // var customer = customercode.map(o => o.label);
    // this.setState({ customer });

    var customer1 = customercode.label;
    this.setState({ customer: customer1 });

    console.log(`Option selected1:`, this.state.customer);
    this.getCustomerName();

    // console.log("Rec2:" + document.getElementById("CustomerCode").innerText);
  };

I am running function this.getCustomerName() in this function, The code for that is this:我在这个函数中运行函数 this.getCustomerName(),代码是这样的:

getCustomerName = () => {
    var Items = [];

    var code = this.state.customer;
    console.log('CustomerCode:' + code);
    axios.post('https://localhost:44303/api/observation/GetCustomerName?' + '&code=' + code).then(response => {
      //console.log('Value=' + response.data);
      console.log('Response=' + response.data);
      response.data.map(item => {
        var obj = new Object();

        // obj.label = desc;
        obj.label = item;

        Items.push(obj);
        //this.setState({ locations: response.data });
      });
      this.setState({ customerName: Items });

      console.log('customerName:' + this.state.customerName);
    });
  };

I am setting code= this.state.customer in the function getCustomerName which I am running just after this.setState({ customer: customer1 });我在函数 getCustomerName 中设置 code= this.state.customer ,我在 this.setState({ customer: customer1 }); 之后运行它。 in handleChangeCC function.在 handleChangeCC 函数中。

However as this.setState is an async function it is not updating the state immediately as a result of which I am getting code as null and my getCustomerName function does not work但是,由于 this.setState 是一个异步函数,它不会立即更新状态,因此我将代码设为 null 并且我的 getCustomerName 函数不起作用

How do I get around This?我该如何解决这个问题? Is there a way to get the variable value of one function to another function.有没有办法将一个函数的变量值传递给另一个函数。 Please help请帮忙

I would pass the customercode into your getCustomerName() method, in this instance.在这种情况下,我会将customercode传递到您的getCustomerName()方法中。 setState will take a moment, so it's value won't be immediately available to your method. setState需要一些时间,因此它的值不会立即对您的方法可用。 You would have to wait for the value to be in state and then call the method.您必须等待该值处于状态,然后调用该方法。 By setting its' state and passing it to your method, you don't have to wait for the state update to complete.通过设置其状态并将其传递给您的方法,您不必等待状态更新完成。

Also, if you're just learning React I would look into using function based components instead of the legacy class components, if you have the option.另外,如果你只是在学习 React,我会考虑使用基于函数的组件而不是遗留的类组件,如果你有选择的话。

Yes, there are several techniques to get the latest value是的,有几种方法可以获取最新值

2nd argument in setState setState 中的第二个参数

this.setState({ customerName: Items }, () => {
    console.log('latest value', this.state.customerName);
});

The second argument that can optionally be passed to setState is a callback function that gets called immediately after the setState is completed and the components get re-rendered.可以选择传递给 setState 的第二个参数是一个回调函数,它在 setState 完成并重新渲染组件后立即调用。

More here更多在这里

async methods异步方法

  updateA = async () => {
    this.setState({
      a: "test"
    });
    await (() => {})(); // or fetch or any async operation
    console.log(this.state.a);
  };

Do note this isn't recommended approach however current version of react supports it.请注意,这不是推荐的方法,但是当前版本的 react 支持它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM