Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.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 如何从React Native中的三个不同选项有条件地进行渲染?_Javascript_Reactjs_React Native - Fatal编程技术网

Javascript 如何从React Native中的三个不同选项有条件地进行渲染?

Javascript 如何从React Native中的三个不同选项有条件地进行渲染?,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我想有条件地在React Native中呈现,但有三种不同的选项:加载屏幕、用户没有消息或显示所有用户消息。我有一个名为conversations的变量,它是从我的数据库中提取的,但是在提取完成之前它是空的,所以我想显示一个加载指示器。获取完成后,如果用户没有当前聊天记录,我想显示一条消息;如果用户没有现有聊天记录,我想显示一个包含所有用户聊天记录的表。我的想法是这样的: return ( <View> {conversations === null &&

我想有条件地在React Native中呈现,但有三种不同的选项:加载屏幕、用户没有消息或显示所有用户消息。我有一个名为conversations的变量,它是从我的数据库中提取的,但是在提取完成之前它是空的,所以我想显示一个加载指示器。获取完成后,如果用户没有当前聊天记录,我想显示一条消息;如果用户没有现有聊天记录,我想显示一个包含所有用户聊天记录的表。我的想法是这样的:

return (
  <View>
    {conversations === null && <LoadingIndicator />} //The loading indicator will be present only when conversartions is null
    {conversations !== null && conversations.length === 0 ? ( // The conversations.length will be evaluated only when conversations is different to null
      <Text>You have no messages.</Text>
    ) : (
      <ConversationTable conversations={conversations} />
    )}
  </View>
)
render() { 
   ...
   let view = null;
   if (conversation == null) {
           view = <ActivityIndicator />
   } else {
       if (conversation.length == 0) 
           view = <Text>You have no messages.</Text>
       else
           view = <ConversationTable conversations={conversations} />
   }       

   ...
   return (
       <View>
           {view}
       </View>
   )
}
回来 {conversations?conversations.length==0? 你没有留言。 : : }
有更好的方法吗?

您可以这样做:

return (
  <View>
    {conversations === null && <LoadingIndicator />} //The loading indicator will be present only when conversartions is null
    {conversations !== null && conversations.length === 0 ? ( // The conversations.length will be evaluated only when conversations is different to null
      <Text>You have no messages.</Text>
    ) : (
      <ConversationTable conversations={conversations} />
    )}
  </View>
)
render() { 
   ...
   let view = null;
   if (conversation == null) {
           view = <ActivityIndicator />
   } else {
       if (conversation.length == 0) 
           view = <Text>You have no messages.</Text>
       else
           view = <ConversationTable conversations={conversations} />
   }       

   ...
   return (
       <View>
           {view}
       </View>
   )
}

您可以这样做:

return (
  <View>
    {conversations === null && <LoadingIndicator />} //The loading indicator will be present only when conversartions is null
    {conversations !== null && conversations.length === 0 ? ( // The conversations.length will be evaluated only when conversations is different to null
      <Text>You have no messages.</Text>
    ) : (
      <ConversationTable conversations={conversations} />
    )}
  </View>
)
render() { 
   ...
   let view = null;
   if (conversation == null) {
           view = <ActivityIndicator />
   } else {
       if (conversation.length == 0) 
           view = <Text>You have no messages.</Text>
       else
           view = <ConversationTable conversations={conversations} />
   }       

   ...
   return (
       <View>
           {view}
       </View>
   )
}
你可以这样走

{conversations ? (
            {conversations.length===0? (
                <Text>You have no messages.</Text>
            ):(
                <ConversationTable conversations={conversations} />
            )}
        ) :(
            <ActivityIndicator />
        )}
你可以这样走

{conversations ? (
            {conversations.length===0? (
                <Text>You have no messages.</Text>
            ):(
                <ConversationTable conversations={conversations} />
            )}
        ) :(
            <ActivityIndicator />
        )}

在我看来,如果在JSX中使用分支,可能会给跟踪代码带来一些混乱和混乱

所以我的建议是把你的if分支放在报税表之外,比如:

return (
  <View>
    {conversations === null && <LoadingIndicator />} //The loading indicator will be present only when conversartions is null
    {conversations !== null && conversations.length === 0 ? ( // The conversations.length will be evaluated only when conversations is different to null
      <Text>You have no messages.</Text>
    ) : (
      <ConversationTable conversations={conversations} />
    )}
  </View>
)
render() { 
   ...
   let view = null;
   if (conversation == null) {
           view = <ActivityIndicator />
   } else {
       if (conversation.length == 0) 
           view = <Text>You have no messages.</Text>
       else
           view = <ConversationTable conversations={conversations} />
   }       

   ...
   return (
       <View>
           {view}
       </View>
   )
}

在我看来,如果在JSX中使用分支,可能会给跟踪代码带来一些混乱和混乱

所以我的建议是把你的if分支放在报税表之外,比如:

return (
  <View>
    {conversations === null && <LoadingIndicator />} //The loading indicator will be present only when conversartions is null
    {conversations !== null && conversations.length === 0 ? ( // The conversations.length will be evaluated only when conversations is different to null
      <Text>You have no messages.</Text>
    ) : (
      <ConversationTable conversations={conversations} />
    )}
  </View>
)
render() { 
   ...
   let view = null;
   if (conversation == null) {
           view = <ActivityIndicator />
   } else {
       if (conversation.length == 0) 
           view = <Text>You have no messages.</Text>
       else
           view = <ConversationTable conversations={conversations} />
   }       

   ...
   return (
       <View>
           {view}
       </View>
   )
}

在JSX之外执行分支逻辑,但仍然在render方法而不是内联方法中,这可能更清楚。例如:如果conversations&&conversations.length返回;,在JSX之外执行分支逻辑,但仍然在render方法而不是内联方法中,这可能更清楚。例如:如果conversations&&conversations.length返回;,以此类推。这实际上不起作用,因为如果对话为空,则对话!==null&&conversations.length==0将计算为false,这将导致呈现ConversationTable。这实际上不起作用,因为如果conversations为null,那么conversations!==null&&conversations.length==0将计算为false,这将导致呈现ConversationTable。此代码存在语法错误,能否像那样嵌套JSX表达式?此代码存在语法错误,能否像那样嵌套JSX表达式?