Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-native: how to override the default style defined in a component

Tags:

Let's say I want to use a Component with below defined style:

var styles = StyleSheet.create({
  container: {
    backgroundColor: '#ffffff'
  },
});

Can I override the base style with mine like this?

<Component style={backgroundColor: 'transparent'}></Component>

It didn't work for me, so wonder if this depends on if the Component supports the behavior.

like image 762
jason chen Avatar asked Nov 28 '15 01:11

jason chen


People also ask

How do you overwrite styles in React Native?

To override style with React Native, we set the style prop to the styles we want by putting them in an object. const styles = StyleSheet. create({ CircleShapeView: { width: 50, height: 50, borderRadius: 50 / 2, backgroundColor: "#000", }, }); //...

How do you override a style on a React component?

To override a style, you have two options. Either you can pass a style object, or you can pass a function that will obtain props regarding the current internal state of the component, thus enabling you to modify styles dynamically depending on the component state, such as isError or isSelected .

How do I override CSS properties in React?

With react-bootstrap , the table element can be overridden using the custom CSS classes, but before using the table, you need to import it. After importing the table element and bootstrap CSS, create the basic table.

How do you override styles in material UI?

If you want to override a component's styles using custom classes, you can use the className prop, available on each component.


1 Answers

The description is a bit vague. Is it a component you wrote your self? Assuming this it should work like this:

export default React.createClass({
  render: function(){
    return(<View style={[ styles.container, this.props.style ]}>...</View>
  }
})

var styles = StyleSheet.create({
  container: {
    backgroundColor: '#ffffff'
  },
});
like image 91
d-vine Avatar answered Oct 01 '22 09:10

d-vine