/
Counter.js
62 lines (57 loc) · 1.78 KB
/
Counter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { StyleSheet, View, Text, TouchableHighlight } from 'react-native';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import * as CounterActions from '../actions/counter';
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
text: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
});
@connect(
state => state,
dispatch => bindActionCreators(CounterActions, dispatch)
)
export default class Counter extends Component {
static propTypes = {
incrementAsync: PropTypes.func.isRequired,
increment: PropTypes.func.isRequired,
decrement: PropTypes.func.isRequired,
incrementIfOdd: PropTypes.func.isRequired,
counter: PropTypes.number.isRequired,
};
constructor(props) {
super(props);
const { incrementAsync } = props;
this.incrementAsync = () => incrementAsync();
}
render() {
const { increment, decrement, incrementIfOdd, counter } = this.props;
return (
<View style={styles.container}>
<Text style={styles.text}>Clicked: {counter} times</Text>
<TouchableHighlight onPress={increment}>
<Text style={styles.text}>+</Text>
</TouchableHighlight>
<TouchableHighlight onPress={decrement}>
<Text style={styles.text}>-</Text>
</TouchableHighlight>
<TouchableHighlight onPress={incrementIfOdd}>
<Text style={styles.text}>Increment if odd</Text>
</TouchableHighlight>
<TouchableHighlight onPress={this.incrementAsync}>
<Text style={styles.text}>Increment async</Text>
</TouchableHighlight>
</View>
);
}
}