覆盖反应原生风格

时间:2018-02-16 09:39:02

标签: css reactjs react-native stylesheet

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

做正确的方法是什么?

2 个答案:

答案 0 :(得分:3)

要覆盖backgroundColor,您可以这样做:

<Image
style={[ styles.CircleShapeView, { backgroundColor: "#fff" } ]}                   
...
/> 

更灵活的覆盖样式的方法是将额外的样式道具传递给子组件。

您可以这样调用您的子组件:

<Subcomponent passedStyle={{ backgroundColor: '#fff' }} /> 

将传递的样式应用于您的图片:

<Image
style={[ styles.CircleShapeView, this.props.passedStyle ]}                   
...
/> 

答案 1 :(得分:0)

要在此处添加其他答案,请确保在尝试覆盖的组件上的样式之后指定继承的道具样式。

示例: 这样做:

<Image
   style={[ styles.CircleShapeView, this.props.passedStyle ]}                   
   ...
/> 

不是这样的:

<Image
   style={[ this.props.passedStyle, styles.CircleShapeView ]}                   
   ...
/> 

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