Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically decide to pass a prop to a react component or not?

I'm using KeyboardAvoidingView and I want to pass a prop 'behaviour' to it in case the platform is IOS and none in case it's android. I don't wanna write two separate JSX component declarations for the same. Is there a way to decide to pass a prop or not. Also react-native's docs don't mention any default value for the behaviour props which I could have set conditionally otherwise.

Based on the platform the component should be declared the following way

FOR IOS
<KeyboardAvoidingView behavior="padding" style={modalContainer} >
  {this.props.children} //I've lots of children here and they're composed inside of the keyboardavoidingview and i'm not using this.props.children as they're in the same parent component
</KeyBoardAvoidingView>


FOR ANDROID
<KeyboardAvoidingView style={modalContainer} >
  {this.props.children}
</KeyBoardAvoidingView>
like image 258
Pavan Avatar asked Dec 03 '22 11:12

Pavan


1 Answers

You can conditionally pass a prop by passing the value or undefined

<KeyboardAvoidingView behavior={platform="android"? "padding": undefined} style={modalContainer} >
  {this.props.children}
</KeyBoardAvoidingView>
like image 89
Shubham Khatri Avatar answered Dec 06 '22 09:12

Shubham Khatri