繁体   English   中英

覆盖反应本机样式

[英]override react native style

我想在react 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 ]}                   
   ...
/> 

第二个示例不会覆盖组件上的样式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM