Skip to main content

useSortableList

List-agnostic reorder state management hook. Returns a SortableListHandle that you wire to SortableContainer, SortableItem, and your list component.

Import

import { useSortableList } from 'react-native-drax';

Usage

const sortable = useSortableList({
data: items,
keyExtractor: (item) => item.id,
onReorder: ({ data }) => setItems(data),
});

Options

OptionTypeDefaultDescription
idstringauto-generatedDraxView id for the container
dataT[]requiredArray of items
keyExtractor(item: T, index: number) => stringrequiredUnique key per item
onReorder(event: SortableReorderEvent<T>) => voidrequiredCalled after reorder
horizontalbooleanfalseHorizontal list layout
numColumnsnumber1Number of columns (grid)
reorderStrategySortableReorderStrategy'insert''insert' or 'swap'
longPressDelaynumber250ms before drag activates
lockToMainAxisbooleanfalseLock drag to list's main axis
autoScrollJumpRationumber0.2Auto-scroll jump as fraction of container
autoScrollBackThresholdnumber0.1Back auto-scroll threshold
autoScrollForwardThresholdnumber0.9Forward auto-scroll threshold
animationConfigSortableAnimationConfig'default'Animation preset or custom config
inactiveItemStyleViewStyleStyle applied to all non-dragged items during drag
itemEnteringEntryOrExitLayoutTypeReanimated layout animation for items entering the list
itemExitingEntryOrExitLayoutTypeReanimated layout animation for items exiting the list
onDragStart(event: SortableDragStartEvent<T>) => voidDrag started
onDragPositionChange(event: SortableDragPositionChangeEvent<T>) => voidPosition changed
onDragEnd(event: SortableDragEndEvent<T>) => voidDrag ended

Return Value: SortableListHandle<T>

PropertyTypeWire To
dataT[]List data prop
onScrollscroll handlerList onScroll prop
onContentSizeChangesize handlerList onContentSizeChange prop
stableKeyExtractor(item: T, index: number) => stringList keyExtractor prop
_internalSortableListInternal<T>Used by SortableContainer and SortableItem

Wiring Example

const sortable = useSortableList({ data, keyExtractor, onReorder });

<SortableContainer sortable={sortable} scrollRef={listRef}>
<FlatList
ref={listRef}
data={sortable.data}
keyExtractor={sortable.stableKeyExtractor}
onScroll={sortable.onScroll}
onContentSizeChange={sortable.onContentSizeChange}
renderItem={({ item, index }) => (
<SortableItem sortable={sortable} index={index}>
<ItemCard item={item} />
</SortableItem>
)}
/>
</SortableContainer>