How to use Platform.OS to elements in react native?

Just Ahead picture Just Ahead · Jul 20, 2018 · Viewed 45.8k times · Source

I want to use just KeyboardAwareScrollView without any functions on IOS and given below code for android.

I know that I need to use Platform.OS === 'ios' ? :

BUT I DON'T UNDERSTAND HOW TO REALISE IT. Please help me

render(){
 return(

    <KeyboardAwareScrollView
       extraScrollHeight={100}
       enableOnAndroid={true}
       keyboardShouldPersistTaps='handled'
    >
      <TextInput
        style={styles.inputContainerStyle}
        label="Username"
        value={this.state.username}
        onChangeText={username => this.setState({ username })}
      />
    </KeyboardAwareScrollView>
   )
}

What I've tried below: (But it doesn't work)

<KeyboardAwareScrollView
      Platform.OS === 'android' ? 
      (
         extraScrollHeight={100}
         enableOnAndroid={true}
         keyboardShouldPersistTaps='handled'
      ) : null
  >

Answer

Farhan picture Farhan · Oct 17, 2018

You can also check via node but react-native provides some ways to check platform.

This one is recommended

 import {Platform} from 'react-native';
    st styles = StyleSheet.create({
      container: {
        flex: 1,
        ...Platform.select({
          ios: {
            backgroundColor: 'red',
          },
          android: {
            backgroundColor: 'blue',
          },
        }),
      },
    });

You can also use ternary expressions

 import {Platform, StyleSheet} from 'react-native';

const styles = StyleSheet.create({
  height: Platform.OS === 'ios' ? 200 : 100,
});

Read this