-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
143 lines (132 loc) · 3.15 KB
/
App.js
File metadata and controls
143 lines (132 loc) · 3.15 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import React, { useState, useEffect } from 'react'
import {
FlatList,
StyleSheet,
Text,
TextInput,
TouchableOpacity,
View,
} from 'react-native'
import Item from './components/Item'
import Constants from 'expo-constants'
import AsyncStorage from '@react-native-async-storage/async-storage'
export default function App() {
const [data, setData] = useState('')
const [validInput, setValidInput] = useState(false)
const [input, setInput] = useState()
const onTextChange = (value) => {
// Disable the button if the input text is less than 2 characters
setValidInput(value.length >= 3)
setInput(value)
}
const addData = () => {
const id = new Date().getTime().toString()
const item = { id: id, name: input }
setData([...data, item])
setInput(null)
}
const deleteData = (id) => {
let items = [...data]
let newData = items.filter((item) => {
if (item.id !== id) {
return item
}
})
setData(newData)
}
const storeData = async () => {
try {
const stringified = JSON.stringify(data)
await AsyncStorage.setItem('listData', stringified)
} catch (e) {
consol.log(e)
}
}
const getData = async () => {
try {
const stringified = await AsyncStorage.getItem('listData')
setData(stringified !== null ? JSON.parse(stringified) : [])
} catch (e) {
consol.log(e)
}
}
useEffect(() => {
if (!data) {
getData()
} else {
storeData()
}
}, [data])
const Renderer = ({ item }) => (
<Item text={item.name} delete={deleteData} id={item.id} />
)
return (
<View style={styles.container}>
<Text style={styles.title}>First flatlist</Text>
<View style={styles.header}>
<TextInput
style={styles.input}
value={input}
onChangeText={onTextChange}
placeholder="Min 3 characters"
/>
<TouchableOpacity
style={validInput ? styles.button : styles.buttonDisabled}
disabled={validInput ? false : true}
onPress={addData}
>
<Text style={styles.btnText}>Add to the list</Text>
</TouchableOpacity>
</View>
<FlatList
data={data}
renderItem={Renderer}
keyExtractor={(item) => item.id}
/>
</View>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: Constants.statusBarHeight,
backgroundColor: '#8ed1fa',
alignItems: 'center',
justifyContent: 'center',
},
title: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 15,
},
header: {
display: 'flex',
flexDirection: 'row',
marginHorizontal: 10,
marginBottom: 10,
},
input: {
backgroundColor: '#FFFFFF',
fontSize: 20,
borderColor: '#DDDDDD',
borderWidth: 1,
padding: 5,
flex: 1,
borderTopLeftRadius: 10,
borderBottomLeftRadius: 10,
},
button: {
backgroundColor: '#000000',
borderTopRightRadius: 10,
borderBottomRightRadius: 10,
},
buttonDisabled: {
backgroundColor: '#9e9d9d',
borderTopRightRadius: 10,
borderBottomRightRadius: 10,
},
btnText: {
color: 'white',
padding: 10,
},
})