Hello Friends đź‘‹,

Welcome To Infinitbility! ❤️

This tutorial will help you to detect device is emulator or not in ios, android, and windows, here we will use react-native-device-info ( https://github.com/react-native-device-info/react-native-device-info ) for detection device is emulator or not.

Let’s start today tutorial How to detect device is emulator in react native

Introduction

React Native device info provide isEmulator() function to check app running in emulator or not it will return only true or false. isEmulator() function work in ios, android, and windows.

Installation

  • Go to your project directory and install react-native-device-info package using npm.
npm install --save react-native-device-info
  • Link react-native-device-info package if your react native version less then 0.63.
npx react-native link react-native-device-info

Syntax

DeviceInfo.isEmulator() function usages syntax.


import DeviceInfo from 'react-native-device-info';


DeviceInfo.isEmulator().then((isEmulator) => {
  if (!isEmulator) {
    // ...
  }
});

Example

In Below Example, we will isEmulator() value on constant and show in application.

React Native Device Info isEmulator example

import React, { Component } from 'react';
import {
  StyleSheet,
  View,
  Text,
  SafeAreaView,
} from 'react-native';
import DeviceInfo from 'react-native-device-info';

class App extends Component {

  constructor(props) {
    super(props);

    this.state = {
      isEmulator: false
    }
  }
  

  componentDidMount(){
    DeviceInfo.isEmulator().then((isEmulator) => {
      if (isEmulator) {
        this.setState({isEmulator: true});
      }
    });
  }

  

  render() {
    const { isEmulator } = this.state;
    return (
      <SafeAreaView style={{ flex: 1 }}>
        <View style={styles.container}>
          <Text>App running in emulator </Text>
          <Text>{`${isEmulator}`}</Text>
        </View>
      </SafeAreaView>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    marginTop: 20,
    backgroundColor: '#ffffff',
  },
});

export default App;

Output

React Native, DeviceInfo, isBatteryCharging, Example

Thanks for Reading…