- Hands-On Design Patterns with React Native
- Mateusz Grzesiukiewicz
- 177字
- 2021-08-13 15:13:04
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.
推薦閱讀
- Learning Cython Programming
- Vue.js入門與商城開發(fā)實(shí)戰(zhàn)
- GitLab Repository Management
- Learning Laravel 4 Application Development
- Unity 5.x By Example
- Android Native Development Kit Cookbook
- 精通Linux(第2版)
- Building RESTful Python Web Services
- Oracle 18c 必須掌握的新特性:管理與實(shí)戰(zhàn)
- Getting Started with Polymer
- Everyday Data Structures
- Oracle實(shí)用教程
- Scala Functional Programming Patterns
- 超好玩的Scratch 3.5少兒編程
- 計(jì)算語言學(xué)導(dǎo)論