React native 如果在本机中有其他项

React native 如果在本机中有其他项,react-native,if-statement,React Native,If Statement,我是个新来的本地人。我已经创建了一个屏幕,在这里我使用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_inf

我是个新来的本地人。我已经创建了一个屏幕,在这里我使用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\u information\u data[0]=null或者说是空的 然后根据if-else,我的else条件是'phpid==null'应该运行 但即使“data.retail\u information\u data[0]=null”我的else条件也没有运行我的控制台kepp 'TypeError:undefined不是对象(正在评估'data.retail\u information\u data[0].id')'
我的if-else中有任何问题,或者如何正确编写if-else

请尝试使用三元运算符。
phpid=数据。零售信息数据[0]?data.retail\u information\u data[0]。id:null

下面的命令应该有效

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

然后它说phpid是只读的是的,你不能直接设置状态。我对类组件不太了解。就像使用功能组件一样,您可以使用setPhpid更新状态。谢谢它的工作我只是在php id thanku之前添加了const,而不是它不断地出现同样的错误