Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Platform OS is not working suddenly

Tags:

react-native

I am using Platform.OS to check ios and android. It is working fine, But suddenly it returns me 0 instead of 'ios' or 'android'. After some RND I found that when the app is loading the first time, it returns me 'android' but when I switch the screen and check Platform.OS then it returns me 0 value. I am wondered why it is happening.

I am sharing the console result.

when the app is reloading the first time.

 LOG  Platform.OS
 LOG  android

after that navigate to the other screen and then check Platform OS.

LOG  Platform.OS 
LOG  0

I am sharing the code snippet to check Platform

import { Platform } from 'react-native';

componentDidMount() {
        console.log('Platform.OS');
        console.log(Platform.OS);
}
like image 870
Rahul dev Avatar asked Nov 25 '25 09:11

Rahul dev


2 Answers

It looks like you used Platform.OS "=" instead of "==" someplace within the code. Example :

wrong way:

(Platform.OS ='android')?console.log("this the wrong way):console.log('Oops')

right way

(Platform.OS=='android')?console.log("Youpi it works):console.log('in your dream, this gonna happen again')
like image 161
Jihad Kahil Avatar answered Nov 28 '25 01:11

Jihad Kahil


The same thing happened to me.

I noted that in one place I have checked platform condition using a single equal operator by mistake like

paddingTop: (Platform.OS = 'android'? 16 : 4)

and then whenever I checked the platform, I always got 16.

So make sure if anywhere you have check condition, like Platform.OS = 'android' replace it with Platform.OS == 'android'

like image 20
Rajesh Nasit Avatar answered Nov 28 '25 02:11

Rajesh Nasit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!