user14877357
user14877357

Reputation:

How to write If else Properly in react native

I have created a Screen. in the screen I got message like "isConnected = true". Now I want to navigate to another screen only if "isConnected = true" else I want to show message like please connect to internet. so what should I write in if else

here is my code

 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>
  );
}

Upvotes: 0

Views: 55

Answers (1)

Prateek Thapa
Prateek Thapa

Reputation: 4938

You could check for the condition and navigate accordingly.

 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>
  );
}

Upvotes: 1

Related Questions