javascript - 如何动态决定是否将 Prop 传递给 react 组件?

标签 javascript reactjs react-native

我正在使用 KeyboardAvoidingView,如果平台是 IOS,我想将一个 Prop “行为”传递给它,如果是 android,则没有。我不想为此编写两个单独的 JSX 组件声明。有没有办法决定是否通过 Prop 。此外,react-native 的文档没有提及我本可以有条件地设置的行为 Prop 的任何默认值。

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>

最佳答案

您可以通过传递 valueundefined

有条件地传递 prop
<KeyboardAvoidingView behavior={platform="android"? "padding": undefined} style={modalContainer} >
  {this.props.children}
</KeyBoardAvoidingView>

关于javascript - 如何动态决定是否将 Prop 传递给 react 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48822490/

相关文章:

react-native - 如何在 React Native 中从 Java 写入 console.log

javascript - 在 React 中继承常见的 props 和方法的最佳实践是什么?

reactjs - 在每个屏幕中显示底部选项卡导航器

javascript - 如何更改 QuickTime 影片文件的尺寸

javascript - Symfony 表单 AJAX 返回空对象

javascript - 使用jquery中的stetimeout函数循环settimeout

javascript - 在挂载之前 react 处理空对象的最佳方法?

javascript - 我如何使用 Reactjs 模板?

javascript - Jasmine 抛出一个错误 - 错误 : Timeout - Async callback was not invoked

javascript - react-router 返回一个页面 你如何配置历史记录?