- Hands-On Design Patterns with React Native
- Mateusz Grzesiukiewicz
- 201字
- 2021-08-13 15:13:00
Touchable opacity
When a button needs a custom look, it quickly seems like you need a better alternative. This is where TouchableOpacity comes into play. It serves every purpose when inner content needs to become touchable. Hence, we will make our own button and style it as we like:
class LikeCounter extends React.Component {
state = {
likeCount: 0
}
like = () => this.setState({likeCount: this.state.likeCount + 1})
unlike = () => this.setState({likeCount: this.state.likeCount - 1})
render = () => (
<View style={styles.container}>
<TouchableOpacity
style={styles.button}
onPress={this.unlike}
>
<Text>Unlike</Text>
</TouchableOpacity>
<Text style={styles.text}>{this.state.likeCount}</Text>
<TouchableOpacity
style={styles.button}
onPress={this.like}
>
<Text>Like</Text>
</TouchableOpacity>
</View>
);
}
Some example styles follow. We will dig further into styles in Chapter 3, Styling Patterns:
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
paddingTop: 20,
paddingLeft: 20
},
button: {
alignItems: 'center', // horizontally centered
justifyContent: 'center', // vertically centered
backgroundColor: '#DDDDDD',
padding: 20
},
text: {
fontSize: 45
}
});
The button's contents are centered vertically and horizontally. We have a custom gray background color and padding inside of the button. Padding is the space from the children to the border of the component.
Now that we know about these simple components, we are ready to proceed further and explore how forms are built and how to handle more complicated use cases.
推薦閱讀
- Mastering Concurrency Programming with Java 8
- 精通JavaScript+jQuery:100%動(dòng)態(tài)網(wǎng)頁設(shè)計(jì)密碼
- LabVIEW 2018 虛擬儀器程序設(shè)計(jì)
- BeagleBone Media Center
- 編寫整潔的Python代碼(第2版)
- GitLab Repository Management
- Visual Basic程序設(shè)計(jì)教程
- 零基礎(chǔ)學(xué)Python數(shù)據(jù)分析(升級(jí)版)
- The HTML and CSS Workshop
- UML 基礎(chǔ)與 Rose 建模案例(第3版)
- Hands-On Full Stack Development with Go
- Swift Playgrounds少兒趣編程
- Solr Cookbook(Third Edition)
- Mastering Backbone.js
- Java程序設(shè)計(jì)教程