if else in react native



我是新手。我已经创建了一个屏幕,在这里我使用if else函数,就像这样。

constructor(props) {
super(props);
this.state={
phpid = ''
}
}
validateInputs = (event) => {

const data = this.props.route.params.data
const { phpid } = this.state;

if (data.retail_information_data[0] != '') {
phpid == data.retail_information_data[0].id  
}else{
phpid == null
}

AsyncStorage.multiGet(["application_id", "created_by"]).then(response => {

console.log(phpid)
fetch('https://njdnfech/Android_API_CI/uploaddata/tffmerchant_retail_details?query=', {

body: JSON.stringify([{phpid : phpid}])
})
.then((returnValue) => returnValue.json())
.then(function(response) {
console.log(response)
return response.json();
}

,但我得到=数据。Retail_information_data [0] = null,或者说是空然后根据if else我的else条件是'phpid == null'应该运行但即使是数据。Retail_information_data [0] = null' my else condition not running my console keep说'TypeError: undefined不是一个对象(求值'data.retail_information_data[0].id')'是否有任何问题在我的if else或如何写正确的if else

下面应该可以…

if (data && data?.retail_information_data.length) {
this.setState(phpid: data?.retail_information_data[0]?.id)
}else{
this.setState(phpid: null)
}

尝试使用三元运算符。php = data。retail_information_data[0]吗?data.retail_information_data[0]。Id: null

最新更新