First, modify the way items are structured to include a completed
state. We’ll adjust the addItem
, deleteItem
, and other relevant functions accordingly.
Updated Code for App.js
Here’s the updated code:
javascriptCopy codeimport React, { useEffect, useState } from 'react';
import { StyleSheet, Text, View, FlatList, TextInput, Button, TouchableOpacity, Alert } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
const App = () => {
const [items, setItems] = useState([]);
const [textInput, setTextInput] = useState('');
const [editIndex, setEditIndex] = useState(null);
useEffect(() => {
const loadItems = async () => {
try {
const storedItems = await AsyncStorage.getItem('items');
if (storedItems) {
setItems(JSON.parse(storedItems));
}
} catch (error) {
console.error(error);
}
};
loadItems();
}, []);
useEffect(() => {
const saveItems = async () => {
try {
await AsyncStorage.setItem('items', JSON.stringify(items));
} catch (error) {
console.error(error);
}
};
saveItems();
}, [items]);
const addItem = () => {
if (textInput.trim()) {
const newItem = { key: textInput, completed: false };
if (editIndex !== null) {
const updatedItems = items.map((item, index) =>
index === editIndex ? newItem : item
);
setItems(updatedItems);
setEditIndex(null);
} else {
setItems([...items, newItem]);
}
setTextInput('');
}
};
const deleteItem = (itemToDelete) => {
setItems(items.filter(item => item.key !== itemToDelete));
};
const confirmDelete = (item) => {
Alert.alert(
'Delete Item',
`Are you sure you want to delete "${item.key}"?`,
[
{ text: 'Cancel', style: 'cancel' },
{ text: 'OK', onPress: () => deleteItem(item.key) },
],
{ cancelable: true }
);
};
const toggleCompletion = (item) => {
const updatedItems = items.map(i =>
i.key === item.key ? { ...i, completed: !i.completed } : i
);
setItems(updatedItems);
};
const startEditing = (item, index) => {
setTextInput(item.key);
setEditIndex(index);
};
return (
<View style={styles.container}>
<Text style={styles.title}>My Item List</Text>
<TextInput
style={styles.input}
placeholder="Add or edit an item"
value={textInput}
onChangeText={setTextInput}
/>
<Button title={editIndex !== null ? "Update Item" : "Add Item"} onPress={addItem} />
<FlatList
data={items}
renderItem={({ item, index }) => (
<TouchableOpacity onLongPress={() => confirmDelete(item)} onPress={() => toggleCompletion(item)}>
<Text style={[styles.item, item.completed && styles.completedItem]}>
{item.key}
</Text>
<Button title="Edit" onPress={() => startEditing(item, index)} />
</TouchableOpacity>
)}
keyExtractor={(item) => item.key}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 20,
backgroundColor: '#fff',
},
title: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 20,
},
input: {
height: 40,
borderColor: 'gray',
borderWidth: 1,
marginBottom: 10,
paddingHorizontal: 10,
},
item: {
padding: 10,
fontSize: 18,
borderBottomColor: '#ccc',
borderBottomWidth: 1,
},
completedItem: {
textDecorationLine: 'line-through',
color: 'gray',
},
});
export default App;
Leave a Reply