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

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.
主站蜘蛛池模板: 夏津县| 招远市| 屏南县| 桂林市| 志丹县| 沙湾县| 喜德县| 城市| 苗栗县| 澜沧| 扎赉特旗| 彰化县| 宿州市| 凯里市| 大方县| 南漳县| 丹巴县| 临桂县| 鲁山县| 彭泽县| 金华市| 门源| 资阳市| 宜昌市| 武冈市| 博白县| 枣阳市| 化隆| 漯河市| 都匀市| 浪卡子县| 阿尔山市| 晋宁县| 称多县| 宝鸡市| 沁水县| 武安市| 马尔康县| 孟村| 荆州市| 林西县|