I see that in react native if I set a fontFamily
in a, for example, a View
, the inner elements do not inherit it the property. Is it there a cascade concept available in react native styling? How do I accomplish it?

- 5,670
- 11
- 47
- 77
-
RN cascades styles to subcomponents for you if you use the style property, you can read more about it here - https://facebook.github.io/react-native/docs/style.html – Matt Aft Dec 23 '16 at 21:26
-
2@MattAft yep, it says 'One common pattern is to make your component accept a style prop which in turn is used to style subcomponents. You can use this to make styles "cascade" the way they do in CSS.' but I don't know exactly what does this mean. – R01010010 Dec 23 '16 at 21:28
-
I edited my answer to show you... – Maxwelll Dec 23 '16 at 21:30
-
@R01010010 I submitted and answer below. – Matt Aft Dec 23 '16 at 21:35
-
1Are you trying to avoid setting font all the time for each
? If so, check [this docs section](https://facebook.github.io/react-native/docs/text.html#limited-style-inheritance), which describes a little trick - create a custom text component with predefined font styles and use it in your application instead of – Michael Radionov Dec 23 '16 at 21:36.
5 Answers
The simplest way I have found to avoid repeating the style prop is to encapsulate in a component. For example:
const Item = ({ text }) => <Text style={{ padding: 10 }}>{text}</Text>
const Consumer = () =>
<View>
<Item text="Item 1" />
<Item text="Item 2" />
<Item text="Item 3" />
</View>

- 1,095
- 12
- 24

- 1,681
- 16
- 15
I've achieved something like this by extracting style objects. Example below.
globalStyle.js
import {StyleSheet} from 'react-native';
export default StyleSheet.create({
myView: {
some view styles
},
myText: {
some text styles
},
});
localStyle.js
import {StyleSheet} from "react-native";
import globalStyle from "../globalStyle";
export default StyleSheet.create({
...globalStyle,
myLocalView: {
local view styles
},
});
anotherLocalStyle.js
import {StyleSheet} from "react-native";
import {myText} from "../globalStyle";
export default StyleSheet.create({
myLocalText: {
...myText,
local text styles
},
});

- 1,095
- 12
- 24

- 174
- 1
- 11
Apparently at this moment a component can only inherit a property of a parent only if the parent is of the same type or at least also support the property to inherit. I was setting fontFamily in the main View
component and wasn't being inherited in the Text
children.

- 5,670
- 11
- 47
- 77
I've had a similar kind of situation and we created a StyleClass that knows how to style each element type and individual elements by looking into predefined style objects.
We created custom wrappers for all the RN base elements which will all call to the StyleClass that knows how to find that elements base styles and any unique styles for its key/id.
Hope this helps in some way. Quick example might be like:
MyImage.jsx
import React from "react";
import { Image } from "react-native";
import { StylesClass } from "./StylesClass";
const styleClass = new StyleClass();
export class MyImage extends React.Component {
render() {
const { uniqueId, sourceUrl } = this.props;
// finds styles by type "image" for base styles and "key" for unique styles
let imageStyles = stylesClass.getElementStyle("image", uniqueId) || [];
return (
<Image
source={{ uri: sourceUrl }}
style={imageStyles}
/>
);
}
}
StyleClass.js (happy path no null / undefined checks)
const styles = {
image: {
// default imagestyles here
},
...
};
const uniqueStyles = {
image: {
someUniqueId: {
// unique styles for "someUniqueId" here
},
},
};
export class StyleClass {
getElementStyle = (elementType, id) => {
return [ ...styles[elementType], ...uniqueStyles[elementType][id] ];
}
}
Then just use MyImage wherever you need Image.
Not necessarily cascading, more like css styling by id but our case had dynamic content coming in that needed to be styled and we didn't really have a cascading solution so we used default styles and then unique styles by each elements ID.

- 1,095
- 12
- 24

- 471
- 5
- 9
Pass styles as props to replicate the cascade in CSS. All child components can inherit a style
prop to achieve this. It would be accomplished like:
const styles = StyleSheet.create({
instructions: {
fontSize: 16,
textAlign: 'center',
margin: 15,
},
});
render() {
return (
<View style={styles.container}>
<Text style={styles.instructions}>
<ChildComponent
cascadingStyle={styles}
/>
</View>
);
}
Here is an example I created to demonstrate rnplay.org/apps/dErxuQ

- 1,095
- 12
- 24

- 2,174
- 1
- 17
- 22
-
1Sorry, how so? that would still force me to set the `style={this.prop.class}` in the child component? cause is precisely what I want to avoid – R01010010 Dec 23 '16 at 21:26
-
Why are you trying to avoid that? Its the simplest way to implement the cascading effect found in CSS and super easy to leverage – Maxwelll Dec 23 '16 at 21:29
-
2I want to avoid in in the children, as in css in html. I set a fontFamily to the parent and the children inherit it automatically. That's simpler. – R01010010 Dec 23 '16 at 21:34
-
this isnt html @R01010010... here is an example of what I am talking about https://rnplay.org/apps/dErxuQ – Maxwelll Dec 23 '16 at 21:42
-
This is no different than assigning the child component styles.instructions. I've also never seen "cascadingStyle="... is that just for example? – James Mar 28 '17 at 20:14
-