-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
48 lines (40 loc) · 1.42 KB
/
App.js
File metadata and controls
48 lines (40 loc) · 1.42 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
import React, {useState} from 'react';
import {StyleSheet, View, Button, FlatList} from 'react-native';
import GoalItem from "./Components/GoalItem";
import GoalInput from "./Components/GoalInput";
const styles = StyleSheet.create({
screen: {
padding: 30,
},
});
const App = () => {
const [courseGoals, setCourseGoals] = useState([]);
const [isModalOpen, setIsModalOpen] = useState(false);
const addGoalHandler = (goalTitle) => {
setCourseGoals(currentGoals => [...currentGoals, {key: Math.random().toString(), val: goalTitle}]);
setIsModalOpen(false);
};
const removeGoalHandler = (key) => {
setCourseGoals(currentGoals => currentGoals.filter(el => el.key !== key));
};
const openModal = () => {
setIsModalOpen(true);
};
const closeModal = () => {
setIsModalOpen(false);
};
return (
<View style={styles.screen}>
<Button title="Add New Goal" onPress={openModal}/>
<GoalInput closeModal={closeModal} visible={isModalOpen} onAddGoal={addGoalHandler}/>
<FlatList
keyExtractor={(item, index) => item.key}
data={courseGoals}
renderItem={itemData =>
<GoalItem
onDelete={() => removeGoalHandler(itemData.item.key)}
title={itemData.item.val}/>}/>
</View>
);
};
export default App;