React Native Platform OS is not working suddenly - 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);
}

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')

Same thing happened with me.
I noted that in one place I have check platform condition using single equal operator by mistake like
paddingTop: (Platform.OS = 'android'
? 16
: 4)
and then after whenever I have checked platform, I always getting 16.
So make sure if anywhere you have check condition, like Platform.OS = 'android' replace it with Platform.OS == 'android'

As #Rajesh Nasit exposed, I had a similar problem, when I wrote
console.log("Platform.OS:" + Platform.OS)
I allways was getting in the console "Platform.OS: 100%"
And the followed error (in Expo CLient App) => 'Unhandled promised error :Invalid platform was passed "100%" '.
Then after reversed my code and checking all the changes I've made, I found the line what was the responsible for thar error.
I had in my StyleSheet the following line
width: Platform.OS === 'android' ? "100%" : "100%",
I asume React-Native somehow set the value "100%" to the Platform.OS.
At final, I resolved that error as follow:
const styles = StyleSheet.create({
imagebackground: {
...Platform.select({
ios: {
width: "100%",
},
android: {
width: "100%",
},
default: {
width: "100%",
}
}),
marginLeft: 0,
},
Well I hope someone find this answer usefull.

Related

OnChangeText still doesn't work correctly

I want to limit TextInput to full number and the value must be less than 50. I'm leveraging OnChangeText event to intercept the input value and filter it accordingly. It kind of works but the issue is, it briefly displays the text before erasing it. This is an excerpt of my code -
const [userInputValue, setuserInputValue] = useState('');
let onChangeTextInput = (value) => {
const numericRegex = /^([0-9])+$/
if(numericRegex.test(value) && Number(value) <= 50) {
setuserInputValue(value)
}
}
return (
<TextInput
placeholder='Type full number less than 50'
onChangeText={newValue => onChangeTextInput(newValue)}
value={ userInputValue }
textAlign='center'
keyboardType='numeric'
style={{
width: 250,
marginVertical: 10
}}
/>
When I searched online, I found this issue was reported as bug for react-native 0.54.3 and supposedly fixed in 0.57.1 (Bug Report). I'm on 0.70.5. I'm wondering if there's any work around for this problem.
System - iOS
React-native - 0.70.5

react native ios QRCodeScanner error ( camera not authorized )

I'm using
"react-native": "0.63.2",
"react-native-camera": "^3.37.0",
'camera not authorized' is the error displayed when the camera is launched
same is true with different camera libraries, but default camera works normally
I've added:
{
NSCameraUsageDescription
NSPhotoLibraryUsageDescription
NSMicrophoneUsageDescription
NSPhotoLibraryAddUsageDescription
}
to ios/project/Info.plist
and also tried:
cameraProps={{captureAudio: false}}
but its not working. Any insights will be much appreciated. Thank you.
Same thing happened with me.
I noted that in one place I have check platform condition using single equal operator by mistake like
paddingTop: (Platform.OS = 'android'
? 16
: 4)
then after changed it to (replacing single equal with double equal while platform condition matching)
paddingTop: (Platform.OS == 'android'
? 16)
: 4)

How do I use ColorFilter with React-Native-Lottie?

I cannot seem to get the colorFilter prop working with my .json file.
There are no errors but the colours are clearly not changing.
<LottieView
style={{
width: 90,
height: 90,
}}
colorFilters={
[
{
keypath: "asdf",
color: "#abcdef",
}
]
}
source={badge.icon}
loop={false}
/>
I'm importing the .json from After Effects using BodyMovin
but am I changing the layer name correctly? If not why on earth is this not working?
I can't get colorFilters to work but you can try doing the following:
import myAnimation from "./../img/myAnimation.json";
in the render part use LottieView with the import and not with the require inline
<LottieView
//source={require("./../img/radius.json")}
source={myAnimation}
/>
Now you can manipulate the properties directly from the json object, just for example here I manipulate the color based on the actual color (from black to white or from white to black)
myAnimation.layers[1].shapes[0].it[1].c.k = myAnimation.layers[1].shapes[0].it[1].c.k[0] == 0 ? [1,1,1,1] : [0,0,0,1];
To find out what is the property you need, check the json, it is not very clear but with some trial and error you can understand the structure.
Just try with react native, and it's work fine for me
<LottieView
style={style.heartLootie}
source={require("../../assets/animations/favorite_animation.json")}
progress={animationProgress}
colorFilters={[
{ keypath: "Red Heart", color: themeColors.PRIMARY },
{ keypath: "Grey Heart", color: themeColors.SECONDARY },
]}
/>
This is an outstanding issue, I have opened an issue here and will work with the devs to hopefully resolve this issue. It is probably the biggest bottleneck for React Native adoption right now.

How to modify the padding in SafeAreaView?

I am trying to position element in the left-upper corner of the screen (there is some margin around). Because of the notch on the iPhone X I am using SafeAreaView. Unfortunately the SafeAreaView padding is enormous, it goes way out of the status bar / notch area. Because of that the element which should visually be in the corner it's now much lower than on other devices.
I looked into StatusBar.currentHeight, but that is supported only for Android. Another option was to use https://github.com/react-native-community/react-native-safe-area-view which has a forceInset parameter. Unfortunately, setting it as { top: xx } makes it work like a normal View with a top padding for all devices (the ones without notch as well).
How can I have a SafeAreaView but with a modified top padding?
I had a similar problem months ago, so I wrote an util to give me the correct height of top/bottom SafeArea to add as padding to a normal react-native View, and then play with them by adding/removing more padding. Here is the code:
import { Dimensions, Platform } from 'react-native'
export function isIphoneX () {
const iphoneXLength = 812
const iphoneXSMaxLength = 896
const windowDimensions = Dimensions.get('window')
return (
Platform.OS === 'ios' &&
!Platform.isPad &&
!Platform.isTVOS &&
(windowDimensions.width === iphoneXLength ||
windowDimensions.height === iphoneXLength ||
windowDimensions.width === iphoneXSMaxLength ||
windowDimensions.height === iphoneXSMaxLength)
)
}
const DimensionsStyle = {
safeAreaTopHeight: Platform.OS === 'ios' ? (isIphoneX() ? 44 : 20) : 0,
safeAreaBottomHeight: Platform.OS === 'ios' && isIphoneX() ? 35 : 0,
tabBarHeight: Platform.OS === 'ios' ? 17 : 20,
bottomAreaHeight: Platform.OS === 'ios' && isIphoneX() ? 34 : 0
}
export default DimensionsStyle
This code works because we know that iPhone X and iPhone XS have a 812p height, and iPhone XSMax and XR have 896p height.
Then you can simply import this util into your view and use it like this:
import Dimension from '../../../utils/DimensionUtils' // path of the util
//
// rest of the code
//
const styles = StyleSheet.create({
container: {
paddingTop: Dimension.safeAreaTopHeight // here you can edit the height of the safeare :))
}
})

How to create react-vr "enter vr button/cardboard"?

I'm playing around with the react-VR framework and can't seem to find anything in the docs about entering vr mode on chrome/cardboard ? Any help or pointers much appreciated.
Here is my part of the code while creating a WebVR tour
<View>
<Pano source={asset(this.state.current_scene['scene_image'])} onInput={this.onPanoInput.bind(this)}
onLoad={this.sceneOnLoad} onLoadEnd={this.sceneOnLoadEnd}
style={{ transform: [{translate: [0, 0, 0]}] }}/>
{this.state.current_scene['navigations'].map(function(item,i){
return <Mesh key={i}
style={{
layoutOrigin: [0.5, 0.5],
transform: [{translate: item['translate']},
{rotateX: item['rotation'][0]},
{rotateY: item['rotation'][1]},
{rotateZ: item['rotation'][2]}]
}}
onInput={ e => that.onNavigationClick(item,e)}>
<VrButton
style={{ width: 0.15,
height:0.15,
borderRadius: 50,
justifyContent: 'center',
alignItems: 'center',
borderStyle: 'solid',
borderColor: '#FFFFFF80',
borderWidth: 0.01
}}>
<VrButton
style={{ width: that.state.animationWidth,
height:that.state.animationWidth,
borderRadius: that.state.animationRadius,
backgroundColor: '#FFFFFFD9'
}}>
</VrButton>
</VrButton>
</Mesh>
})}
</View>
onNavigationClick(item,e){
if(e.nativeEvent.inputEvent.eventType === "mousedown" && e.nativeEvent.inputEvent.button === 0){
var new_scene = this.state.scenes.find(i => i['step'] === item.step);
this.setState({current_scene: new_scene});
postMessage({ type: "sceneChanged"})
}
}
sceneOnLoad(){
postMessage({ type: "sceneLoadStart"})
}
sceneOnLoadEnd(){
postMessage({ type: "sceneLoadEnd"})
}
this.sceneOnLoad = this.sceneOnLoad.bind(this);
this.sceneOnLoadEnd = this.sceneOnLoadEnd.bind(this);
this.onNavigationClick = this.onNavigationClick.bind(this);
Maybe you will find something helpful for you also here
for desktop Chrome there is flag: chrome://flags/#enable-webvr
after you enable it and restart Chrome the button "View in VR" will appear, but unfortunately I didn't see any changes when I click it.
I also have tried it on experimental developers build of Chromium, but there was even more flags that should be enabled.
a lot of stuff about tuning your browser is here:
https://superuser.com/questions/836832/how-can-i-enable-webgl-in-my-browser
chrome://gpu/ is very handy to check your 3D environment and don't forget that in some cases (usually on laptops) you can run your browser on integrated videocard instead of powerful GPU, so resluts could be extremly different.
and finally you can check source code of node_modules/ovrui/dist/ovrui.js function attemptEnterVR() to understand better what should happens when you try to switch to VR mode.
Like Pavel Sokolov said, by default WebVR is not enabled and requires updating in the chrome://flags section. This will enable the Show in VR button. However, the current version of Chrome (57) has issues with WebVR, which may result in a black screen for you. In that case, try out Chrome Canary instead, I've had success using it.
I wrote a guide about getting my Cardboard up and running with React-VR, you can read it here.