0

I am pretty new to react-native. Whenever i have to make use of SafeAreaView I have to make a seperate .android.js file for same component without safe area leading to duplication.

Is it possible to conditionally use SafeAreaView with platform.os?

amar
  • 4,285
  • 8
  • 40
  • 52

3 Answers3

1

Yes, It is possible to use SafeAreaView with Conditionally for Platform.OS. SafeAreaView only applicable on ios, but also work on android. On my code SafeAreaView work on both OS. If there is a need only on a particular OS then give condition.

Abhishek Sutariya
  • 112
  • 1
  • 1
  • 6
1

I have same problem. So what I done is that create one component called whatever you want eg. SafeScrollView.js and then render conditionally SafeAreaView in that component then pass children to that component.

For example :

I have SafeScrollView.js like below :

import React from 'react';
import { View, SafeAreaView, Platform } from 'react-native';

const SafeScrollView = (props) => {
    if (Platform.OS === "ios") {
        return (
            <View style={props.style}>
                {props.children}
            </View>
        );
    }
    return (
        <SafeAreaView style={props.style}>
            {props.children}
        </SafeAreaView>
    )
}

export default SafeScrollView

Then use SafeScrollView as a component like below :

<SafeScrollView>
     // Your component
</SafeScrollView>

Now, In IOS it will render normal View component and if device is android it will render SafeScrollView. So, you don't have to create separate file.

Kishan Bharda
  • 5,446
  • 3
  • 30
  • 57
0

SafeAreaView only supports ios. So, you can use conditional statement to you this for particular os.

ManojK
  • 171
  • 1
  • 4
  • I know that and I have already mentioned that i have to make seperate .android.js file. How can i conditionally use in ios and plain in android. – amar Aug 06 '19 at 05:19