[Nomard] React Native - Delete
Delete
- 삭제하는 버튼에서 deleteTodo 함수를 실행시켜보자.
<TouchableOpacity onPress={() => deleteTodo(key)}>
<Text>❌</Text>
</TouchableOpacity>
- 오브젝트를 delete 메서드를 이용해서 없애면 된다.
const deleteTodo = async (id) => {
const newToDos = { ...toDos };
delete newToDos[id];
setToDos(newToDos);
await saveToDos(newToDos);
};
Alert
- import
import { StyleSheet, Text, View, Alert } from 'react-native';
- use
const deleteTodo = (id) => {
Alert.alert('Delete To Do', 'Are you Sure?', [
{ text: 'Cancel' },
{
text: 'Confirm',
style: 'destructive',
onPress: () => {
const newToDos = { ...toDos };
delete newToDos[id];
setToDos(newToDos);
saveToDos(newToDos);
},
},
]);
};
출처: https://reactnative.dev/docs/alert#alert