如何动态决定将prop传递给反应组件?

Pav*_*van 3 javascript reactjs react-native

我正在使用KeyboardAvoidingView,我希望传递一个道具'行为',以防平台是IOS,如果它是android的话,没有.我不想为同一个编写两个单独的JSX组件声明.有没有办法决定是否通过道具.反应本机的文档也没有提到行为道具的任何默认值,否则我可以有条件地设置.

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>
Run Code Online (Sandbox Code Playgroud)

Shu*_*tri 9

你可以通过传递value或有条件地传球undefined

<KeyboardAvoidingView behavior={platform="android"? "padding": undefined} style={modalContainer} >
  {this.props.children}
</KeyBoardAvoidingView>
Run Code Online (Sandbox Code Playgroud)