react-native - 我如何在 react-native 中覆盖 css 属性
问题描述
我已经创建了自定义按钮, <View style={styles.buttonContainer}> <AppButton title="comes in" color="yellow" /> <AppButton title="register now" color="white" /> </View>
当我保存我的代码时,两个按钮都给了我相同的颜色,所以在我创建自定义按钮组件的地方我修改了背景颜色 return ( <TouchableHighlight style={[styles.button, { backgroundColor: colors[color] }]} > <Text style={styles.text}>{title}</Text> </TouchableHighlight> ); }
,但问题是这样我想修改marginVertical
export default function AppButton({ title, color = "", marginVertical = "" }) { return ( <TouchableHighlight style={[styles.button, { backgroundColor: colors[color] ,marginVertical=[marginVertical]}]} > <Text style={styles.text}>{title}</Text> </TouchableHighlight> ); }
并且在欢迎屏幕中我这样称呼它<View style={styles.buttonContainer}> <AppButton title="comes in" color="yellow" /> <AppButton title="register now" color="white" marginVertical="4"/> </View>
解决方案
要覆盖 marginVertical 您可以像下面一样简单地传递它,而不是提供一个键并使用一个数组。
function AppButton({ title, color = '', marginVertical = 0 }) {
return (
<TouchableHighlight
style={[
styles.button,
{ backgroundColor: colors[color], marginVertical:marginVertical },
]}>
<Text style={styles.text}>{title}</Text>
</TouchableHighlight>
);
}
此外,如果您打算使用多种样式,请使用单个样式对象并覆盖它,如下所示
function AppButton({ title, style={} }) {
return (
<TouchableHighlight
style={[
styles.button,
style,
]}>
<Text style={styles.text}>{title}</Text>
</TouchableHighlight>
);
}
推荐阅读
- python - AttributeError:“WebDriver”对象没有属性“驱动程序”
- java - 将一个表中的多个外键引用到另一个表中的相同值并读取该值
- java - Mac Java 11 与 Java 8 dylib dlopen 错误:无法加载 libzstd.1.dylib
- python - 在窗口中居中许多小部件 [tkinter]
- android - android中形状的角颜色
- javascript - 如何让 identity.launchWebAuthFlow 从浏览器扩展弹出窗口而不是后台脚本工作?
- apache-spark - 在 pyspark 中过滤值不相等
- django-forms - 如何显示表单字段以便用户填写
- korge - 如何镜像龙骨动画?
- javascript - 反应错误:未处理的拒绝(TypeError):this.state.categories.map 不是函数