简体   繁体   English

如何根据状态值在反应中获取数据

[英]how to fetch data in react based on the state value

on a button-click i am changing a value of a state property based on that value i want to fetch the data.单击按钮时,我正在根据要获取数据的值更改状态属性的值。 Issue I am facing is that, data is fetched before the state change even after calling it after the set state method.我面临的问题是,即使在 set state 方法之后调用数据,也会在状态更改之前获取数据。 Due to which data is fetched based on the previous value of the state.由于根据状态的先前值获取数据。 (new to react please help) (新手反应请帮忙)

If I understood it right.如果我理解正确的话。 You want to use the updated value of a state.您想使用状态的更新值。

You can use callback method of setState.您可以使用 setState 的回调方法。

For example -例如 -

this.setState({item1: 25}, ()={
 // now use your updated state in your code...
 console.log(this.state.item1); // will give you new value of item1
})

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

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