首页 > 解决方案 > 如果其他反应原生

问题描述

我是新来的反应本地人。我已经创建了一个屏幕,我正在使用 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();
}
  

但我得到 = data.retail_information_data[0] = null 或者让我们说空然后根据 if else 我的 else 条件是 'phpid == null' 应该运行,但即使是 'data.retail_information_data[0] = null' 我的 else 条件没有运行我的控制台kepp说'TypeError:undefined is not an object(评估'data.retail_information_data [0].id')'是我的if else中的任何问题,或者如何写正确if else

标签: react-nativeif-statement

解决方案


尝试使用三元运算符。phpid = data.retail_information_data[0] ?data.retail_information_data[0].id:空


推荐阅读