Search code examples
cssreactjsreact-nativestylesheet

override react native style


I want to override some sub-component of style in react native.like - I have created an style CircleShapeView

 const styles = StyleSheet.create({
      CircleShapeView: {
        width: 50,
        height: 50,
        borderRadius: 50/2,
        backgroundColor: '#000'
    },
    });

I want change backgroundColor ,when i am usins this style. some thing like this.

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

What is right way to do it?


Solution

  • To override the backgroundColor, you could just do this:

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

    A more flexible way of overriding style would be to pass an additional style prop to your subcomponent.

    You would call your subcomponent like that:

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

    Apply passed style to your image:

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