Javascript 检查状态是否为真或假
我想检查状态是否为真,如果为真,我会将数据发送到modal 所以我想在setState完成更新后执行一个函数 这是我的代码:Javascript 检查状态是否为真或假,javascript,reactjs,Javascript,Reactjs,我想检查状态是否为真,如果为真,我会将数据发送到modal 所以我想在setState完成更新后执行一个函数 这是我的代码: getPriceData = () => { if(this.state.visibleModal){ const id = this.props.product._id; DataController.getProduct(id).then( response => this.setState({
getPriceData = () => {
if(this.state.visibleModal){
const id = this.props.product._id;
DataController.getProduct(id).then(
response =>
this.setState({
product: response,
buyingPrice: response.price
})
);
}
}
<Button title='ADD TO CART' id={_id}
buttonStyle={{ backgroundColor: 'darkgreen', marginRight: 10 }}
containerStyle={{ width: 120}}
onPress={this._toggleModal&&this.getPriceData}/>
getPriceData=()=>{
if(this.state.visibleModel){
const id=此.props.product.\u id;
DataController.getProduct(id)。然后(
响应=>
这是我的国家({
产品:回应,,
buyingPrice:response.price
})
);
}
}
您可以尝试使用componentdiddupdate()
React中的这个.setState
是异步的
。因此,您可以在setState中定义一个回调函数来处理如下操作:
this.setState({
product: response,
buyingPrice: response.price
}, () => {
// here, your state updated
console.log('state updated: ', this.state)
})
<Button
title='ADD TO CART'
id={_id}
buttonStyle={{ backgroundColor: 'darkgreen', marginRight: 10 }}
containerStyle={{ width: 120}}
onPress={this._toggleModal}
/>
this.setState({
product: response,
buyingPrice: response.price
}, () => {
// here, your state updated
console.log('state updated: ', this.state)
})