如何在 React Native 中编写等效的 IF / ELSE 语句



我想在 React Native 中写出这个等价物:

if(response){
    if(status ==='ERROR'){
        return 'Error'
    }else{
        return 'Response Returned'
    }
}

我尝试了以下方法:

{
  Object.keys(this.props.myStore.response).length !==0  ? 
  (this.props.myStore.status ==='ERROR') ? 
    <Text>error</Text> : 
    <Text>Response Returned</Text>
}

对于代码,我收到错误,我是 React Native 的新手,但已经尝试了上述代码的变体,但不断收到错误。任何将此代码块改进为"最佳实践"的进一步建议将不胜感激。

终于让它工作了,代码如下:

{
      Object.keys(this.props.myStore.response).length ===0 ?
      null : 
      this.props.myStore.status ==='ERROR' ? 
        <Text>error</Text> : 
        <Text>Response Returned</Text>
    }

你应该试试这个

( status === 'ERROR') ? 'Error' : 'Response Returned'

最新更新