react-native-sortables icon indicating copy to clipboard operation
react-native-sortables copied to clipboard

[Bug]: Empty space / missing item after adding or replacing data; fixed by starting a drag

Open julian-gargicevich opened this issue 5 months ago • 5 comments

Description

When the data of a Sortable.Grid changes in a way that is not a reorder — appending a new item (add), or replacing an item in place while keeping the same keyExtractor key — the grid's internal layout sometimes goes stale:

  • an empty gap is left where a row should be, or
  • the replaced item renders blank / disappears.

The grid never re-measures the affected row on its own. The bug fixes itself the instant a drag gesture starts — beginning a drag forces a re-measure/relayout and the gap or missing item immediately snaps back into place.

So it appears that structural data changes that don't change item order don't reliably trigger a re-measure of the affected row, and the drag handler is what forces it.

This looks closely related to the (closed) #286 — "New items are sometimes placed in the wrong position" / "the last item added is invisible".

Expected behavior

After adding or replacing an item, the grid should lay out the new/changed row correctly without requiring a drag gesture to trigger a relayout.

Notes / workaround

Keeping a stable keyExtractor key across reorders is intentional in our app (so a slot keeps its drag identity when its content is replaced). Our current workaround is to bump a React key on the Sortable.Grid itself — forcing a full remount — only when an add/replace happens (detected via an order-independent signature of key → itemId), and not on reorder. A remount produces a correct layout, which further suggests the issue is a missed internal re-measure rather than bad data.

Steps to reproduce

  1. Render a single-column Sortable.Grid with a keyExtractor returning a stable per-item id.
  2. From a button (outside the grid), append a new item to data — or replace an existing item with a new one while returning the same keyExtractor key for that slot.
  3. Observe (intermittently) an empty gap where the new/changed row should be, or the replaced row rendered blank.
  4. Start dragging any row — the layout immediately corrects itself.

Code snippet, Snack or GitHub repository link

We have not yet isolated this in the example app (the same difficulty noted in #286 — it reproduces in our app but is intermittent). The grid is used roughly like this:

const keyExtractor = useCallback(
  (item: Item) => item.uid, // stable per-slot id; survives replace
  []
);

<Sortable.Grid
  columns={1}
  data={items}
  dimensionsAnimationType="layout"
  keyExtractor={keyExtractor}
  scrollableRef={scrollRef}
  rowGap={0}
  columnGap={0}
  onDragEnd={({ data }) => setItems(data)}
  renderItem={({ item }) => (
    <Animated.View
      layout={LinearTransition.duration(220)}
      exiting={FadeOut.duration(160)}
    >
      <Row item={item} />
    </Animated.View>
  )}
/>

// elsewhere:
// add:     setItems(prev => [...prev, newItem])
// replace: setItems(prev => prev.map(i => i.uid === target.uid ? { ...replacement, uid: i.uid } : i))

I'm happy to put together a standalone repro if it would help — please let me know.

React Native Sortables version

1.9.4

Reanimated version

4.3.1

React Native Gesture Handler version

2.31.2

React Native version

0.85.3

Platforms

iOS (confirmed via screen recording). Suspected on Android as well — #286 reported the "invisible last item" specifically on Android.

Additional information

  • React: 19.2.3
  • Expo SDK: 56
  • Architecture: Fabric (New Architecture)
  • Workflow: Expo Dev Client
  • dimensionsAnimationType="layout", columns={1}, rowGap={0}, columnGap={0}, rows wrapped in an Animated.View with Reanimated LinearTransition layout + FadeOut exiting animations.

julian-gargicevich avatar Jun 16 '26 11:06 julian-gargicevich

This one looks like a Reanimated mapper-scheduling issue rather than a bug in react-native-sortables. Could you try bumping react-native-reanimated to 4.5.0 (with react-native-worklets 0.10) and let me know if it helps?

MatiPl01 avatar Jun 28 '26 10:06 MatiPl01

I have a similar problem. I don't know if my case relates to the issue, but the problem is also fixed by starting a drag. My elements in Sortable.Grid are expandable list items. When you tap on them, they expand and show their content. By tapping on the content, you can edit it (in another route screen). And occasionally, I encounter an issue on Android where the content 'overlaps' the next element after I lock the device and wait for about 5–7 minutes, then unlock it again.

<Animated.ScrollView
  ref={scrollableRef}
  showsVerticalScrollIndicator={false}
  contentContainerStyle={styles.listContent}
  onScroll={onScroll}
>
  <Sortable.Grid
    columns={1}
    data={items}
    overDrag="vertical"
    customHandle
    keyExtractor={(item) => String(item.id)}
    dragActivationDelay={100}
    scrollableRef={scrollableRef}
    onDragStart={() => {
      Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Heavy);
    }}
    onDragEnd={(result) => {
      const { fromIndex, toIndex } = result;
      if (fromIndex === toIndex) return;
      // Reorder logic here
    }}
    renderItem={({ item, index }) => (
      <>
        <Animated.View
          style={styles.wrapper}
          layout={LinearTransition.duration(300)}
        >
          <List.Item
            title={item.title}
            description={item.description}
            left={() => (
              <View style={styles.iconsContainer}>
                <List.Icon icon={item.expanded ? "chevron-up" : "chevron-down"} />
                {/* Icon placeholder */}
              </View>
            )}
            right={() => (
              <View style={styles.iconsContainer}>
                <IconButton
                  style={styles.icon}
                  icon="dots-vertical"
                  size={24}
                  onPress={() => {}}
                />
                <Sortable.Handle>
                  <IconButton
                    style={styles.icon}
                    icon="drag-horizontal-variant"
                    size={24}
                    onPress={() => {}}
                  />
                </Sortable.Handle>
              </View>
            )}
            onPress={() => {
              // Toggle expand
            }}
            style={styles.listItem}
            borderless
          />

          {item.expanded && (
            <Animated.View
              entering={isMounted ? FadeInUp.duration(300) : undefined}
              exiting={FadeOutUp.duration(300)}
              layout={LinearTransition.duration(300)}
            >
              {/* Expanded content */}
              <TouchableOpacity
                style={styles.expandedContentContainer}
                onPress={() => {}}
              >
                {hasContent ? (
                  // Content items rendered here
                  <View style={styles.contentRow}>
                    <View style={styles.badgeWrapper}>
                      <View style={[styles.badge, { backgroundColor: someColor }]}>
                        <Text style={[styles.badgeText, { color: someTextColor }]}>
                          Label
                        </Text>
                      </View>
                      {hasNote && (
                        <View
                          style={[
                            styles.noteDot,
                            {
                              backgroundColor: theme.colors.primary,
                              borderColor: theme.colors.background,
                            },
                          ]}
                        />
                      )}
                    </View>

                    <View style={styles.contentDataContainer}>
                      <Text style={styles.primaryText}>Value</Text>
                      <Text style={styles.secondaryText}>Unit</Text>
                    </View>
                  </View>
                ) : (
                  <ActionElement
                    variant="add"
                    style={styles.addButton}
                    onPress={() => {}}
                  >
                    Add content
                  </ActionElement>
                )}
              </TouchableOpacity>
            </Animated.View>
          )}
        </Animated.View>
        
        {index < items.length - 1 && <Divider />}
      </>
    )}
  />
</Animated.ScrollView>

V0V45 avatar Jul 03 '26 20:07 V0V45

Thanks for the report. This looks like a bug in reanimated rather than in sortables: the reaction that positions the grid/flex items doesn't reliably re-run when item sizes change in reanimated 4.3.1 (the version Expo SDK 56 ships), which is why starting a drag fixes it.

I tested with newer reanimated (4.5.0 with react-native-worklets 0.10) and couldn't reproduce the problem, so I'd recommend bumping to it. I'm closing this for now - if it still happens after updating, please reopen and I'll look into it further.

MatiPl01 avatar Jul 04 '26 09:07 MatiPl01

Still getting reports of this happening with 4.5.0 and 0.10 :(

julian-gargicevich avatar Jul 31 '26 19:07 julian-gargicevich

Mhm, it may be caused by the fact that library expects different keys for different items. It seems to treat the same key as if you added back the already removed item and places the new item with the same key on the position where the item with this key was placed before.

Does it happen only if you have 2 items with the same key or also when you remove an item and add back an item with the same key later on? Can you possibly share an exact repro for that?

MatiPl01 avatar Jul 31 '26 20:07 MatiPl01