官术网_书友最值得收藏!

The Button component

Button is such a common component that you will find yourself using it in any kind of app. Let's build a small like counter with up and down buttons:

// Chapter 2 / Example 7 / src / LikeCounter.js
class LikeCounter extends React.Component {
state = {
likeCount: 0
}
// like/unlike function to increase/decrease like count in state
like = () => this.setState({likeCount: this.state.likeCount + 1})
unlike = () => this.setState({likeCount: this.state.likeCount - 1})

render = () => (
<View style={styles.container}>
<Button
onPress={this.unlike}
title="Unlike"
/>
<Text style={styles.text}>{this.state.likeCount}</Text>
<Button
onPress={this.like}
title="Like"
/>
</View>
);
}
// Styles omitted for clarity

Further modifications to this concept can implement upvotes/downvotes for comments or a star system for reviews.

The Button component is very limited, and those who are used to web development may be surprised. For instance, you cannot set the text in a web-way, for example,  <Button>Like</Button>nor can you pass the style prop. If you need to style your button, please use  TouchableXXXX. Check out the next section for an example on  TouchableOpacity.
主站蜘蛛池模板: 天津市| 宁国市| 芮城县| 永城市| 图木舒克市| 紫阳县| 衡山县| 七台河市| 高雄市| 清远市| 襄汾县| 湖北省| 内江市| 西安市| 巩留县| 漯河市| 梁平县| 平定县| 铁力市| 桦川县| 义马市| 安远县| 衡南县| 德昌县| 东丽区| 沂水县| 威信县| 隆尧县| 茌平县| 茶陵县| 西林县| 丰都县| 广元市| 会理县| 光泽县| 巴林右旗| 攀枝花市| 甘谷县| 盐源县| 博爱县| 松阳县|