如何在reactjs中的componentDidMount中检索更新的状态元素

如何在reactjs中的componentDidMount中检索更新的状态元素,reactjs,setstate,Reactjs,Setstate,我试图在componentDidMount()中检索更新的状态值,以便在从API获取数据时将该值作为参数附加。我已经创建了这个handleChange()方法,从中可以使用当前所需的值更新状态。问题是componentDidMount()在第一次呈现页面之前命中,因此如果我尝试获取状态值,它会显示null值。我不知道如何提及我希望在第二次渲染后获取值 constructor(props) { super(props) this.state = { names: [],

我试图在componentDidMount()中检索更新的状态值,以便在从API获取数据时将该值作为参数附加。我已经创建了这个handleChange()方法,从中可以使用当前所需的值更新状态。问题是componentDidMount()在第一次呈现页面之前命中,因此如果我尝试获取状态值,它会显示null值。我不知道如何提及我希望在第二次渲染后获取值

constructor(props) {
    super(props)
    this.state = {
      names: [],
      equipments: [],
      isLoaded: false,
      inputValue: null,
      siteID: ''
    };
  }
handleChange = selectedOption => {
    let { inputValue, stateID } = this.state;
    this.setState({ ...this.state, inputValue: selectedOption });
    console.log(`Option selected: ${selectedOption.value}`);
    let selectedElement = selectedOption.value;
    let filteredID = stateData.filter(name => name.name == selectedElement)
      .map((name) => {
        return name.id
      })
    // console.log(filteredID[0]);
    this.setState({ stateID: filteredID[0] })
    localStorage.setItem(stateID, filteredID[0]);
  };

ComponentDidMount() {
    const token = localStorage.getItem("token");
    console.log("Inside Component Drop Down =" + token);

    // let stateid = this.state.stateID;
    // console.log("stateid" + stateid);

    let stateid = 23301;//
    let url2 = `https://applicaiton/api/helpdesk/get_personID/?stateid=${stateid}`;
    fetch(url2, {
      method: "GET",
      headers: { "Content-Type": "application/json", "Authorization": `Token ${token}` },
      credentials: "same-origin"
    })
      .then((results1) => {
        return results1.json();
      }).then(data2 => {
        this.setState({
          isLoaded: true,
          equipments: data2,
        })
      });
  }


而不是<代码>组件Dealths,考虑使用反应生命周期方法<代码>组件Debug UPDATE <代码>如下:

componentDidUpdate(prevProps, prevState, snapshot) {
   // Make comparison between this.state and prevState 
   // as necessary to retrieve proper value
}