-
Notifications
You must be signed in to change notification settings - Fork 0
/
Count.js
64 lines (53 loc) · 1.31 KB
/
Count.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
63
64
import React from 'react'
import {StyleSheet, Text, View} from 'react-native'
import PropTypes from 'prop-types'
const styles = StyleSheet.create({
count: {fontSize: 72},
})
const howManySeconds = (total) => {
return String(total % 60).padStart(2,'0')
}
const howManyMinutes = (total) => {
return Math.floor(total/60)
}
class Count extends React.Component {
static propTypes = {
length: PropTypes.number.isRequired,
}
constructor(props) {
super(props)
this.state = {
count: props.length,
minutes: howManyMinutes(props.length),
seconds: howManySeconds(props.length),
}
}
componentDidMount() {
this.inteval = setInterval(this.inc, 1000)
}
componentWillUnmount() {
clearInterval(this.inteval)
}
inc = () => {
//When not unmounting this appears ever time toggled on
console.log('Increment!')
this.setState(prevState => ({
count: prevState.count === 0 ? 0 : prevState.count - 1,
}),this.convertToDisplay())
}
convertToDisplay = () => {
this.setState({
minutes: howManyMinutes(this.state.count),
seconds: howManySeconds(this.state.count),
})
}
render() {
return (
<View>
<Text style={styles.count}>{this.state.minutes}:{this.state.seconds}
</Text>
</View>
);
}
}
export default Count