Delete
Alert 사용하기
static alert (
title: string,
message?: string,
buttons?: AlertButton[],
options?: AlertOptions,
);
- title : 제목
- message : 메세지
- buttons : 버튼 설정하는데, array 형식으로 저장됨
- options : 다양한 옵션들이 있다.
>> 자세한 내용은 공식문서 참조
https://reactnative.dev/docs/alert#alertbutton
Alert.alert("Delete To Do", "Are you sure?", [
{ text: "Cancel" },
{
text: "Delete",
onPress: () => {
const newToDos = { ...toDos };
delete newToDos[key];
setToDos(newToDos);
saveToDos(newToDos);
},
style: "destructive",
},
]);
- 사용된 코드
전체 소스코드
import { StatusBar } from "expo-status-bar";
import { useEffect, useState } from "react";
import {
StyleSheet,
Text,
View,
TouchableOpacity,
TextInput,
ScrollView,
Alert,
} from "react-native";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { theme } from "./colors";
const STORAGE_KEY = "@toDos";
export default function App() {
const [working, setWorking] = useState(true);
const [text, setText] = useState("");
const [toDos, setToDos] = useState({});
useEffect(() => {
loadToDos();
}, []);
const travel = () => setWorking(false);
const work = () => setWorking(true);
const onChangeText = (payload) => setText(payload);
const saveToDos = async (toSave) => {
await AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(toSave));
};
const loadToDos = async () => {
const s = await AsyncStorage.getItem(STORAGE_KEY);
s !== null ? setToDos(JSON.parse(s)) : null;
};
const addToDo = async () => {
if (text === "") {
return;
}
const newToDos = {
...toDos,
[Date.now()]: { text, working },
};
setToDos(newToDos);
await saveToDos(newToDos);
setText("");
};
const deleteToDo = (key) => {
Alert.alert("Delete To Do", "Are you sure?", [
{ text: "Cancel" },
{
text: "Delete",
onPress: () => {
const newToDos = { ...toDos };
delete newToDos[key];
setToDos(newToDos);
saveToDos(newToDos);
},
style: "destructive", // ios에만 있음
},
]);
};
return (
<View style={styles.container}>
<StatusBar style="auto" />
<View style={styles.header}>
<TouchableOpacity onPress={work}>
<Text
style={{ ...styles.btnText, color: working ? "white" : theme.grey }}
>
Work
</Text>
</TouchableOpacity>
<TouchableOpacity onPress={travel}>
<Text
style={{
...styles.btnText,
color: !working ? "white" : theme.grey,
}}
>
Travel
</Text>
</TouchableOpacity>
</View>
<TextInput
onSubmitEditing={addToDo}
onChangeText={onChangeText}
returnKeyType="done"
value={text}
placeholder={working ? "Add a To Do" : "where do you want to go?"}
style={styles.input}
/>
<ScrollView>
{Object.keys(toDos).map((key) =>
toDos[key].working === working ? (
<View style={styles.toDo} key={key}>
<Text style={styles.toDoText}>{toDos[key].text}</Text>
<TouchableOpacity onPress={() => deleteToDo(key)}>
<Text>❌</Text>
</TouchableOpacity>
</View>
) : null
)}
</ScrollView>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: theme.bg,
paddingHorizontal: 20,
},
header: {
justifyContent: "space-between",
flexDirection: "row",
marginTop: 100,
},
btnText: {
fontSize: 38,
fontWeight: "600",
color: "white",
},
input: {
backgroundColor: "white",
paddingVertical: 15,
paddingHorizontal: 20,
borderRadius: 30,
marginVertical: 20,
fontSize: 18,
},
toDo: {
backgroundColor: theme.toDoBg,
marginBottom: 10,
paddingVertical: 20,
paddingHorizontal: 20,
borderRadius: 10,
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between",
},
toDoText: {
color: "white",
fontSize: 16,
fontWeight: "600",
},
});
'앱 개발 > React-Native' 카테고리의 다른 글
[React-Native] Todo 앱 만들기(5/6)_Persist(AsyncStorage) (0) | 2023.01.28 |
---|---|
[React-Native] Todo 앱 만들기(4/6)_Object.keys() (0) | 2023.01.28 |
[React-Native] Todo 앱 만들기(3/6)_Object.assign(), ES6 (0) | 2023.01.28 |
[React-Native] Todo 앱 만들기(2/6)_TextInput (0) | 2023.01.28 |
[React-Native] Todo 앱 만들기(1/6)_Touchables (0) | 2023.01.28 |