覆盖反应原生风格
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}
...
/>
正确的做法是什么?
要覆盖背景颜色,您可以这样做:
<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 ]}
...
/>
第二个示例不会覆盖组件上的样式。
我想在 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}
...
/>
正确的做法是什么?
要覆盖背景颜色,您可以这样做:
<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 ]}
...
/>
第二个示例不会覆盖组件上的样式。