- Hands-On Design Patterns with React Native
- Mateusz Grzesiukiewicz
- 629字
- 2021-08-13 15:13:03
How React Native styles work
- React library README ( https://github.com/reactjs/react-basic/blob/master/README.md).
You will learn about pure functions later in this book. Check out the following example to understand the basics:
// Code example from React readme. Comments added for clarity.
// JavaScript pure function
// for a given input always returns the same output
function NameBox(name) {
return { fontWeight: 'bold', labelContent: name };
}
// Example with input
'Sebastian Markb?ge' ->
{ fontWeight: 'bold', labelContent: 'Sebastian Markb?ge' };
Going back to more practical examples, let's see how the preceding premise is implemented in React Native.
The style prop can be a plain old JavaScript object. (...) You can also pass an array of styles - the last style in the array has precedence, so you can use this to inherit styles.
As a component grows in complexity, it is often cleaner to use StyleSheet.create to define several styles in one place."
- React Native official documentation ( https://facebook.github.io/react-native/docs/style.html).
To sum up, we have three ways of defining the component style:
- Using style props and passing an object with key-value pairs that represent styles.
- Using style props and passing an array of objects. Each object should contain key-value pairs that represent styles. The last style in the array has precedence. Use this mechanism to inherit styles or shadow them as you would shadow functions and variables.
- Using the StyleSheet component and its create function to create styles.
In the following example, you can find all three ways of defining styles:
// src/ Chapter_3/ Example_1_three_ways_to_define_styles/ App.js
export default () => (
<View>
<Text style={{ color: 'green' }}>inline object green</Text>
<Text style={styles.green}>styles.green green</Text>
<Text style={[styles.green, styles.bigred]}>
[styles.green, styles.bigred] // big red
</Text>
<Text style={[styles.bigred, styles.green]}>
[styles.bigred, styles.green] // big green
</Text>
</View>
);
const styles = StyleSheet.create({
green: {
color: 'green'
},
bigred: {
color: 'red',
fontSize: 35
}
});
Pay attention to the use case with array of objects. You may combine previously-learned tricks to achieve conditional styles:
<View>
<Text
style={[
styles.linkStyle,
this.props.isActive && styles.activeLink
]}
>
Some link
</Text>
</View>
Also, let's discuss why we use the StyleSheet component instead of inline styles:
- Code quality:
- By moving styles away from the render function, you're making the code easier to understand.
- Naming the styles is a good way to add meaning to the low-level components in the render function.
- Performance:
- Making a stylesheet from a style object makes it possible to refer to it by ID instead of creating a new style object every time.
- It also allows you to send the style only once through the bridge. All subsequent uses are going to refer an ID (not implemented yet).
- React Native official documentation
https://facebook.github.io/react-native/docs/stylesheet.html.
When it comes to the quality and reusability, StyleSheet decouples styles and component markup. You could even extract these styles away to a separate file. Also, as mentioned in the documentation, it allows you to make your markup easier to understand. Instead of a huge styling object, you can see a meaningful name, such as styles.activeLink.
- Facebook Application Development with Graph API Cookbook
- HoloLens Beginner's Guide
- MySQL 8 DBA基礎教程
- Visual Basic程序設計(第3版):學習指導與練習
- PLC編程及應用實戰
- Python漫游數學王國:高等數學、線性代數、數理統計及運籌學
- Python時間序列預測
- PHP 7+MySQL 8動態網站開發從入門到精通(視頻教學版)
- Python圖形化編程(微課版)
- 自學Python:編程基礎、科學計算及數據分析(第2版)
- 區塊鏈國產化實踐指南:基于Fabric 2.0
- 計算語言學導論
- Node.js實戰:分布式系統中的后端服務開發
- Visual C#(學習筆記)
- Unity與C++網絡游戲開發實戰:基于VR、AI與分布式架構