-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathApp.js
More file actions
61 lines (55 loc) · 1.4 KB
/
App.js
File metadata and controls
61 lines (55 loc) · 1.4 KB
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
import React, { useState, useEffect } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import RoundedButton from './RoundedButton';
import axios from 'axios';
export default function App() {
const [color, setColor] = useState('#161616');
const [prompt, setPrompt] = useState('Hello!');
useEffect(() => {
randomPrompt();
setColor(randomRgb());
}, '');
const randomPrompt = async () => {
try {
const response = await axios.get('http://localhost:3000/random');
const prompt = response.data;
setPrompt(prompt.title);
} catch(err) {
console.log(err);
}
}
return (
<View style={[styles.container, { backgroundColor: color }]}>
<Text style={styles.prompt}>{prompt}</Text>
<RoundedButton
text="Next"
textColor="#161616"
onPress={() => {
randomPrompt();
setColor(randomRgb());
}}
/>
</View>
);
}
const randomRgb = () => {
const red = Math.floor(Math.random() * 256);
const green = Math.floor(Math.random() * 256);
const blue = Math.floor(Math.random() * 256);
return `rgb(${red}, ${green}, ${blue})`;
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
padding: 20
},
prompt: {
color: 'white',
fontSize: 22,
padding: 20,
textAlign: 'center'
}
});