首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在原生react中正确编写If else

如何在原生react中正确编写If else
EN

Stack Overflow用户
提问于 2021-01-18 20:17:33
回答 1查看 33关注 0票数 0

我已经创建了一个屏幕。在屏幕上,我收到类似"isConnected = true“的消息。现在,我只想导航到另一个屏幕,如果"isConnected = true“,否则我想显示消息,如请连接到互联网。如果不是这样,我应该写什么呢

以下是我的代码

代码语言:javascript
运行
复制
 import React, { Component } from "react";
 import { View, Text, Button, Alert, Platform, alert } from "react-native";
import {useNetInfo} from "@react-native-community/netinfo";

export const NoConnection = ({navigation}) => {
  const netInfo = useNetInfo();

  if (condition) {
    
  } else {
    
  }
 return (
    <View>
       <Text>Type: {netInfo.type}</Text>
      <Text>Is Connected? {netInfo.isConnected.toString()}</Text>
    </View>
  );
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-18 20:19:56

您可以检查条件并相应地导航。

代码语言:javascript
运行
复制
 import React, { Component } from "react";
 import { View, Text, Button, Alert, Platform, alert } from "react-native";
import {useNetInfo} from "@react-native-community/netinfo";

export const NoConnection = ({navigation}) => {
  const netInfo = useNetInfo();

  if (netInfo.isConnected) {
     // navigation -> go to other screen
     return null;  
  }
 return (
    <View>
       <Text>Type: {netInfo.type}</Text>
      <Text>Is Connected? {netInfo.isConnected.toString()}</Text>
    </View>
  );
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65774378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档