Bird
Raised Fist0
React Nativemobile~20 mins

Swipeable list items in React Native - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Swipeable List
A screen showing a list of items that can be swiped left to reveal a Delete button.
Target UI
-------------------------
| Item 1               |
|-----------------------|
| Item 2               |
|-----------------------|
| Item 3               |
|-----------------------|
| Item 4               |
-------------------------
(Swipe left on any item to reveal a red Delete button)
Display a vertical list of at least 4 items with text labels.
Each item can be swiped left to reveal a red Delete button on the right side.
Tapping the Delete button removes the item from the list.
The list updates immediately after deletion.
Use React Native's gesture handler or any suitable library for swipe gestures.
Starter Code
React Native
import React, { useState } from 'react';
import { View, Text, FlatList, StyleSheet } from 'react-native';

export default function SwipeableList() {
  const [items, setItems] = useState([
    { id: '1', text: 'Item 1' },
    { id: '2', text: 'Item 2' },
    { id: '3', text: 'Item 3' },
    { id: '4', text: 'Item 4' },
  ]);

  // TODO: Implement swipeable list items with delete button

  return (
    <View style={styles.container}>
      <FlatList
        data={items}
        keyExtractor={item => item.id}
        renderItem={({ item }) => (
          <View style={styles.item}>
            <Text>{item.text}</Text>
          </View>
        )}
      />
    </View>
  );
}

const styles = StyleSheet.create({
  container: { flex: 1, paddingTop: 50 },
  item: { padding: 20, borderBottomWidth: 1, borderColor: '#ccc' },
});
Task 1
Task 2
Task 3
Solution
React Native
import React, { useState } from 'react';
import { View, Text, FlatList, StyleSheet, TouchableOpacity, Animated } from 'react-native';
import { Swipeable } from 'react-native-gesture-handler';

export default function SwipeableList() {
  const [items, setItems] = useState([
    { id: '1', text: 'Item 1' },
    { id: '2', text: 'Item 2' },
    { id: '3', text: 'Item 3' },
    { id: '4', text: 'Item 4' },
  ]);

  const renderRightActions = (progress, dragX, itemId) => {
    const scale = dragX.interpolate({
      inputRange: [-100, 0],
      outputRange: [1, 0],
      extrapolate: 'clamp',
    });

    return (
      <TouchableOpacity
        onPress={() => handleDelete(itemId)}
        style={styles.deleteButton}
        accessibilityLabel="Delete item"
      >
        <Animated.Text style={[styles.deleteText, { transform: [{ scale }] }]}>Delete</Animated.Text>
      </TouchableOpacity>
    );
  };

  const handleDelete = (id) => {
    setItems(currentItems => currentItems.filter(item => item.id !== id));
  };

  const renderItem = ({ item }) => (
    <Swipeable
      renderRightActions={(progress, dragX) => renderRightActions(progress, dragX, item.id)}
      overshootRight={false}
    >
      <View style={styles.item}>
        <Text>{item.text}</Text>
      </View>
    </Swipeable>
  );

  return (
    <View style={styles.container}>
      <FlatList
        data={items}
        keyExtractor={item => item.id}
        renderItem={renderItem}
        accessibilityLabel="Swipeable list of items"
      />
    </View>
  );
}

const styles = StyleSheet.create({
  container: { flex: 1, paddingTop: 50, backgroundColor: '#fff' },
  item: { padding: 20, borderBottomWidth: 1, borderColor: '#ccc', backgroundColor: '#fff' },
  deleteButton: {
    backgroundColor: 'red',
    justifyContent: 'center',
    alignItems: 'center',
    width: 80,
    height: '100%',
  },
  deleteText: {
    color: 'white',
    fontWeight: 'bold',
  },
});

We used the Swipeable component from react-native-gesture-handler to enable swipe gestures on each list item. When the user swipes left, a red Delete button appears on the right side.

The renderRightActions function creates the Delete button with an animated scale effect for smooth appearance. Pressing the Delete button calls handleDelete, which removes the item from the state array, causing the list to update immediately.

Accessibility labels are added for screen readers. The list uses FlatList for efficient rendering.

Final Result
Completed Screen
-------------------------
| Item 1               |
|-----------------------|
| Item 2               |
|-----------------------|
| Item 3               |
|-----------------------|
| Item 4               |
-------------------------
(Swipe left on any item reveals a red [Delete] button on the right side)
Swipe left on any list item to reveal the red Delete button on the right.
Tap the Delete button to remove that item from the list immediately.
The list updates and shows remaining items.
Stretch Goal
Add an Undo feature that appears as a temporary banner after deleting an item, allowing the user to restore it.
💡 Hint
Use a state to store the last deleted item and a timer to hide the Undo banner after a few seconds.

Practice

(1/5)
1. What is the main purpose of using Swipeable in React Native list items?
easy
A. To allow users to swipe list items and reveal hidden actions
B. To automatically sort list items alphabetically
C. To add animations when list items are tapped
D. To disable scrolling on the list

Solution

  1. Step 1: Understand Swipeable component role

    The Swipeable component enables swipe gestures on list items.
  2. Step 2: Identify the effect of swiping

    Swiping reveals hidden buttons or actions like delete or edit.
  3. Final Answer:

    To allow users to swipe list items and reveal hidden actions -> Option A
  4. Quick Check:

    Swipeable = Reveal actions on swipe [OK]
Hint: Swipeable shows hidden buttons when you swipe list items [OK]
Common Mistakes:
  • Thinking Swipeable sorts items automatically
  • Confusing swipe with tap animations
  • Assuming Swipeable disables scrolling
2. Which prop is used to define the buttons shown when swiping a list item to the right in Swipeable?
easy
A. renderLeftActions
B. renderRightActions
C. onSwipeRight
D. swipeButtons

Solution

  1. Step 1: Recall Swipeable props for swipe actions

    renderLeftActions defines buttons shown when swiping to the right (revealing left side).
  2. Step 2: Match prop to swipe direction

    Swiping to the right reveals buttons from the left side, so renderLeftActions is correct.
  3. Final Answer:

    renderLeftActions -> Option A
  4. Quick Check:

    Right swipe buttons = renderLeftActions [OK]
Hint: Use renderLeftActions for buttons on right swipe [OK]
Common Mistakes:
  • Confusing renderRightActions with right swipe
  • Using non-existent prop onSwipeRight
  • Assuming swipeButtons is a valid prop
3. Given this code snippet, what will happen when the user swipes the list item to the left?
import { Swipeable } from 'react-native-gesture-handler';

function ListItem() {
  return (
     }
    >
      Message
    
  );
}
medium
A. The 'Archive' button appears when swiping left to right
B. No button appears on swipe
C. The 'Archive' button appears when swiping right to left
D. The list item deletes immediately on swipe

Solution

  1. Step 1: Understand renderLeftActions behavior

    renderLeftActions shows buttons when swiping to the right (revealing left side).
  2. Step 2: Analyze swipe to the left

    Swiping to the left reveals right side actions via renderRightActions, which is not provided, so no button appears.
  3. Final Answer:

    No button appears on swipe -> Option B
  4. Quick Check:

    Swipe left requires renderRightActions [OK]
Hint: Swipe left reveals right actions (renderRightActions) [OK]
Common Mistakes:
  • Thinking renderLeftActions shows on swipe left
  • Confusing the swipe direction for left actions
  • Expecting immediate deletion on swipe
4. What is wrong with this code snippet that tries to add swipe actions?
import { Swipeable } from 'react-native-gesture-handler';

function Item() {
  return (
     Delete}
    >
      Item
    
  );
}
medium
A. Swipeable must wrap a View, not Text
B. renderRightActions should be a boolean, not a function
C. Missing onSwipe callback prop
D. Text component cannot be used directly as a swipe action button

Solution

  1. Step 1: Check renderRightActions return type

    Swipe actions should be interactive buttons or touchable components, not plain Text.
  2. Step 2: Identify correct component usage

    Using Text alone won't respond to touches; use TouchableOpacity or Button.
  3. Final Answer:

    Text component cannot be used directly as a swipe action button -> Option D
  4. Quick Check:

    Swipe actions need touchable components [OK]
Hint: Swipe actions must be touchable, not plain Text [OK]
Common Mistakes:
  • Using plain Text instead of touchable components
  • Thinking renderRightActions is a boolean
  • Assuming Swipeable can't wrap Text
5. You want to create a swipeable list where swiping left reveals a 'Delete' button and swiping right reveals an 'Archive' button. Which code snippet correctly implements this behavior?
hard
A. Delete} renderRightActions={() => Archive} > Item
B. } renderRightActions={() => } > Item
C. } renderRightActions={() => } > Item
D. } rightActions={() => } > Item

Solution

  1. Step 1: Match swipe directions to actions

    Swiping left means moving finger leftwards, revealing right side actions, so 'Delete' should be on right.
  2. Step 2: Assign correct props

    renderRightActions shows buttons on right side (swipe left), renderLeftActions shows buttons on left side (swipe right).
  3. Step 3: Check button components

    Buttons are used correctly, not plain Text.
  4. Final Answer:

    renderLeftActions shows 'Archive', renderRightActions shows 'Delete' -> Option C
  5. Quick Check:

    Left swipe = right actions, right swipe = left actions [OK]
Hint: Left swipe reveals right actions; right swipe reveals left actions [OK]
Common Mistakes:
  • Swapping left and right actions
  • Using plain Text instead of buttons
  • Using wrong prop names like leftActions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard