- 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.
- Mastering Ext JS(Second Edition)
- Hyper-V 2016 Best Practices
- 零基礎學C++程序設計
- JavaFX Essentials
- Mastering Python High Performance
- Python數據挖掘與機器學習實戰
- Python完全自學教程
- 軟件測試技術指南
- Java EE 8 Application Development
- 微服務從小白到專家:Spring Cloud和Kubernetes實戰
- Visual Studio 2015高級編程(第6版)
- Scala編程(第5版)
- Clojure Polymorphism
- 大規模語言模型開發基礎與實踐
- SAS編程演義