Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查状态是否为真或假_Javascript_Reactjs - Fatal编程技术网

Javascript 检查状态是否为真或假

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({

我想检查状态是否为真,如果为真,我会将数据发送到modal 所以我想在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)

})