Complete the code to import the component needed for swipeable list items in React Native.
import { [1] } from 'react-native-gesture-handler';
The Swipeable component from react-native-gesture-handler enables swipe gestures on list items.
Complete the code to render a swipeable list item with a right action button.
const renderRightActions = () => (
<View style={{backgroundColor: 'red', justifyContent: 'center', alignItems: 'center', width: 80}}>
<Text style={{color: 'white'}}>Delete</Text>
</View>
);
return (
<[1] renderRightActions={renderRightActions}>
<View style={{padding: 20, backgroundColor: 'white'}}>
<Text>Swipe me</Text>
</View>
</[1]>
);The Swipeable component wraps the list item and accepts renderRightActions to show buttons when swiped.
Fix the error in the code to close the swipeable item after pressing the delete button.
const swipeableRef = useRef(null);
const closeSwipe = () => {
if (swipeableRef.current) {
swipeableRef.current.[1]();
}
};The Swipeable component has a close() method to close the swipe actions programmatically.
Fill both blanks to create a swipeable list item that calls a function when deleted.
const onDelete = () => {
console.log('Item deleted');
};
return (
<Swipeable
ref={swipeableRef}
renderRightActions={() => (
<TouchableOpacity onPress={() => { swipeableRef.current.[1](); [2](); }} style={{backgroundColor: 'red', justifyContent: 'center', alignItems: 'center', width: 80}}>
<Text style={{color: 'white'}}>Delete</Text>
</TouchableOpacity>
)}
>
<View style={{padding: 20, backgroundColor: 'white'}}>
<Text>Swipe me</Text>
</View>
</Swipeable>
);When pressing delete, first call close() on the swipeable ref to close the swipe, then call onDelete() to handle deletion.
Fill all three blanks to create a swipeable list with multiple items and delete functionality.
const [items, setItems] = useState(['Apple', 'Banana', 'Cherry']); const swipeableRefs = useRef(new Map()); const deleteItem = (index) => { swipeableRefs.current.get(index).[1](); setItems(items.filter((_, i) => i !== index)); }; return ( <FlatList data={items} keyExtractor={(item, index) => index.toString()} renderItem={({item, index}) => ( <Swipeable ref={ref => swipeableRefs.current.set(index, ref)} renderRightActions={() => ( <TouchableOpacity onPress={() => deleteItem(index)} style={{backgroundColor: 'red', justifyContent: 'center', alignItems: 'center', width: 80}}> <Text style={{color: 'white'}}>[2]</Text> </TouchableOpacity> )} > <View style={{padding: 20, backgroundColor: 'white'}}> <Text>{ [3] }</Text> </View> </Swipeable> )} /> );
To delete an item, first call close() on its swipeable ref, then update the list. The button text is 'Delete' and the displayed text is the item name.