- 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
- Cross-platform Desktop Application Development:Electron,Node,NW.js,and React
- PostgreSQL 11從入門到精通(視頻教學版)
- Building Mapping Applications with QGIS
- Mastering Google App Engine
- 名師講壇:Spring實戰開發(Redis+SpringDataJPA+SpringMVC+SpringSecurity)
- 單片機應用與調試項目教程(C語言版)
- BeagleBone Black Cookbook
- Go語言精進之路:從新手到高手的編程思想、方法和技巧(2)
- Java高并發核心編程(卷1):NIO、Netty、Redis、ZooKeeper
- Visual Studio 2015高級編程(第6版)
- 寫給程序員的Python教程
- OpenStack Networking Essentials
- Android移動應用開發項目教程