官术网_书友最值得收藏!

Surprising styles inheritance

As we start to use styles, it is vital to understand that React Native styles do not work as a website's CSS. Especially when it comes to inheritance.

Styles of the parent component are not inherited unless it is a Text component. If it is a Text component, it will inherit from parent, only if parent is another Text component:

// src/ Chapter_3/ Example_2_Inheritance_of_Text_component/ App.js

export default
() => (
<View style={styles.container}>
<Text style={styles.green}>
some green text
<Text style={styles.big}>
some big green text
</Text>
</Text>
</View>
);

const styles = StyleSheet.create({
container: {
marginTop: 40
},
green: {
color: 'green'
},
big: {
fontSize: 35
}
});

If you run this code, you will see that the displayed text is green and that the later part is also big. Text with a big style inherited the green colour from the parent Text component. Please also note that the whole text is rendered inside of a View component that has a margin top of 40 dp that is density-independent pixels. Jump to the Learning unitless dimensions section to learn more.

主站蜘蛛池模板: 准格尔旗| 高唐县| 霍城县| 惠水县| 株洲县| 虹口区| 天峻县| 万州区| 无锡市| 江津市| 静海县| 汝阳县| 阿克陶县| 夹江县| 历史| 乌拉特后旗| 富锦市| 郁南县| 闻喜县| 太白县| 鄂托克前旗| 镇远县| 凉山| 余姚市| 汨罗市| 平利县| 吕梁市| 金门县| 盐亭县| 托克逊县| 白山市| 怀宁县| 利津县| 瑞金市| 永安市| 敦化市| 金湖县| 永嘉县| 称多县| 公安县| 义乌市|