我想在native native.like中覆盖样式的一些子组件 - 我创建了一个样式CircleShapeView
const styles = StyleSheet.create({
CircleShapeView: {
width: 50,
height: 50,
borderRadius: 50/2,
backgroundColor: '#000'
},
});
当我使用这种风格时,我想要改变backgroundColor
。这样的事情。
<Image
style={backgroundColor: "#fff", styles.CircleShapeView}
...
/>
什么是正确的方法呢?
要覆盖backgroundColor,您可以这样做:
<Image
style={[ styles.CircleShapeView, { backgroundColor: "#fff" } ]}
...
/>
一种更灵活的覆盖样式的方法是将一个额外的样式道具传递给您的子组件。
您可以像这样调用您的子组件:
<Subcomponent passedStyle={{ backgroundColor: '#fff' }} />
将传递的样式应用于您的图像:
<Image
style={[ styles.CircleShapeView, this.props.passedStyle ]}
...
/>
要在此处添加其他答案,请确保在要尝试覆盖的组件上的样式之后指定继承的道具样式。
示例:执行此操作:
<Image
style={[ styles.CircleShapeView, this.props.passedStyle ]}
...
/>
不是这个:
<Image
style={[ this.props.passedStyle, styles.CircleShapeView ]}
...
/>
第二个示例不会覆盖组件上的样式。