In React you can clearly create an object and assign it as an inline style. i.e.. mentioned below.
var divStyle = {
Unlike React Native, we cannot pass array of styles in React, like
<View style={[style1, style2]} />
In React, we need to create the single object of styles before passing it to style property. Like:
const Header = (props) => {
let baseStyle = {
color: 'red',
}
let enhancedStyle = {
fontSize: '38px'
}
return(
<h1 style={{...baseStyle, ...enhancedStyle}}>{props.title}</h1>
);
}
We have used ES6 Spread operator to combine two styles. You can also use Object.assign() as well for the same purpose.
This also works if you don't need to store your style in a var
<Segment style={{...segmentStyle, ...{height:'100%'}}}>
Your content
</Segment>
I've found that this works best for me. It overrides as expected.
return <View style={{...styles.local, ...styles.fromProps}} />
If you're using React Native, you can use the array notation:
<View style={[styles.base, styles.background]} />
Check out my detailed blog post about this.
You can use the spread operator:
<button style={{...styles.panel.button,...styles.panel.backButton}}>Back</button
const style1 = {
backgroundColor: "#2196F3",
}
const style2 = {
color: "white",
}
const someComponent = () => {
return <div style={{ ...style1, ...style2 }}>This has 2 separate styles</div>
}
Note the double curly brackets. The spread operator is your friend.
Ways of inline styling:
<View style={[styles.red, {fontSize: 25}]}>
<Text>Hello World</Text>
</View>
<View style={[styles.red, styles.blue]}>
<Text>Hello World</Text>
</View>
<View style={{fontSize:10,marginTop:10}}>
<Text>Hello World</Text>
</View>