In React you can clearly create an object and assign it as an inline style. i.e.. mentioned below.
var divStyle = {
I have built an module for this if you want to add styles based on a condition like this:
multipleStyles(styles.icon, { [styles.iconRed]: true })
https://www.npmjs.com/package/react-native-multiple-styles
Object.assign()
is an easy solution, but the (currently) top answer's usage of it — while just fine for making stateless components, will cause problems for the OP's desired objective of merging two state
objects.
With two arguments, Object.assign()
will actually mutate the first object in-place, affecting future instantiations.
Ex:
Consider two possible style configs for a box:
var styles = {
box: {backgroundColor: 'yellow', height: '100px', width: '200px'},
boxA: {backgroundColor: 'blue'},
};
So we want all our boxes to have default 'box' styles, but want to overwrite some with a different color:
// this will be yellow
<div style={styles.box}></div>
// this will be blue
<div style={Object.assign(styles.box, styles.boxA)}></div>
// this SHOULD be yellow, but it's blue.
<div style={styles.box}></div>
Once Object.assign()
executes, the 'styles.box' object is changed for good.
The solution is to pass an empty object to Object.assign()
. In so doing, you're telling the method to produce a NEW object with the objects you pass it. Like so:
// this will be yellow
<div style={styles.box}></div>
// this will be blue
<div style={Object.assign({}, styles.box, styles.boxA)}></div>
// a beautiful yellow
<div style={styles.box}></div>
This notion of objects mutating in-place is critical for React, and proper use of Object.assign()
is really helpful for using libraries like Redux.
Actually, there is a formal way to combine and it is like below:
<View style={[style01, style02]} />
But, there is a small issue, if one of them is passed by the parent component and it was created by a combined formal way we have a big problem:
// The passing style02 from props: [parentStyle01, parentStyle02]
// Now:
<View style={[style01, [parentStyle01, parentStyle02]]} />
And this last line causes to have UI bug, surly, React Native cannot deal with a deep array inside an array. So I create my helper function:
import { StyleSheet } from 'react-native';
const styleJoiner = (...arg) => StyleSheet.flatten(arg);
By using my styleJoiner
anywhere you can combine any type of style and combine styles. even undefined
or other useless types don't cause to break the styling.
To take this one even further, you could create a classnames-like helper function:
const styleRules = (...rules) => {
return rules.filter(Boolean).reduce((result, rule) => {
return { ...result, ...rule };
}, {});
};
And then use it conditionally in your components:
<div style={
styleRules(
divStyle,
(window.innerWidth >= 768) && divStyleMd,
(window.innerWidth < 768) && divStyleSm
)
}>Hello World!</div>
You can also combine classes with inline styling like this:
<View style={[className, {paddingTop: 25}]}>
<Text>Some Text</Text>
</View>