-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
117 lines (109 loc) · 2.57 KB
/
App.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* React Native Basics Part 2 (Buttons, TextInput and State)
* https://github.com/lohanitech/RNProductive
* Author: Damodar Lohani
* @flow
*/
import React, { Component } from 'react';
import {
TextInput,
Text,
View,
ScrollView,
Button,
Image,
StyleSheet,
Platform
} from 'react-native';
import logo from './logo.png';
export default class App extends Component<{}> {
constructor(props){
super(props)
this.state = {
activities: ["Code", "Drink Water", "Eat", "Nap"],
decidedActivity: 0,
addActivityText: ''
}
}
componentWillMount() {
this.decide()
}
decide = () => {
let random = Math.floor(Math.random() * this.state.activities.length)
this.setState({
decidedActivity: random
})
}
handleAddActivity = () => {
if(this.state.addActivityText !== ''){
this.setState((previousState)=> ({
activities: previousState.activities.concat([previousState.addActivityText]),
addActivityText: ''
}))
}
}
render() {
return (
<ScrollView contentContainerStyle={styles.container}>
<Text style={[styles.text, styles.title]}>React Native Productive</Text>
<Image style={styles.image} source={logo} />
<View style={styles.deciderContainer}>
<Text style={{fontSize: 18, color: '#333'}}>What to do next?</Text>
<Text style={styles.decidedText}>{this.state.activities[this.state.decidedActivity]}</Text>
<Button
title="Decide"
onPress={this.decide}
/>
</View>
<View style={styles.addActivityContainer}>
<TextInput
placeholder="add new activity"
style={styles.textInput}
onChangeText={(text)=>this.setState({addActivityText: text})}
value={this.state.addActivityText}
/>
<Button title="Add Activity" onPress={this.handleAddActivity} />
</View>
</ScrollView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#ffffff',
padding: 20,
justifyContent: 'space-between'
},
deciderContainer: {
flex: 3,
alignItems: 'center',
},
addActivityContainer: {
flex: 2,
},
image: {
alignSelf: 'center'
},
textInput: {
...Platform.select({
ios: {
borderBottomWidth: 1,
borderBottomColor: '#eeeeee'
}
})
},
title: {
textAlign: 'center',
fontSize: 24,
fontWeight: '900'
},
text: {
color: '#087f23'
},
decidedText: {
color: '#008080',
fontSize: 28,
padding: 10
}
});