-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
51 lines (45 loc) · 1.35 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
import { StatusBar } from 'expo-status-bar';
import React, { useState } from 'react';
import { Button, FlatList, StyleSheet, View } from 'react-native';
import GoalInput from './components/GoalInput';
import GoalItem from './components/GoalItem';
export default function App() {
const [courseGoals, setCourseGoals] = useState([]);
const [isAddMode, setIsAddMode] = useState(false);
const addGoalHandler = goal => {
setCourseGoals(currentGoals => [
...currentGoals,
{ id: Math.random().toString(), value: goal },
]);
setIsAddMode(false);
};
const removeGoalHandler = goalId => {
setCourseGoals(currentGoals => {
return currentGoals.filter(goal => goal.id !== goalId);
});
};
const cancelGoalAdditionHandler = () => setIsAddMode(false);
return (
<View style={styles.screen}>
<Button title='Add new goal' onPress={() => setIsAddMode(true)} />
<GoalInput
visible={isAddMode}
onAddGoal={addGoalHandler}
onCancel={cancelGoalAdditionHandler}
/>
<FlatList
keyExtractor={(item, index) => item.id}
data={courseGoals}
renderItem={itemData => (
<GoalItem onDelete={removeGoalHandler} goal={itemData.item} />
)}
/>
<StatusBar style='auto' />
</View>
);
}
const styles = StyleSheet.create({
screen: {
padding: 50,
},
});