React Native Reanimated DnDReact Native Reanimated DnD
API ReferenceHooks

useHorizontalSortable

API reference for the useHorizontalSortable hook

Hook for creating individual horizontal sortable items with drag-and-drop reordering capabilities.

Import

import { useHorizontalSortable } from "react-native-reanimated-dnd";

Type Signature

function useHorizontalSortable<T>(
  options: UseHorizontalSortableOptions<T>
): UseHorizontalSortableReturn;

Parameters

UseHorizontalSortableOptions<T>

PropertyTypeRequiredDefaultDescription
idstringYes-Unique identifier for this sortable item
positionsSharedValue<{[id: string]: number}>Yes-Shared value containing positions of all items
leftBoundSharedValue<number>Yes-Current horizontal scroll position
autoScrollDirectionSharedValue<HorizontalScrollDirection>Yes-Auto-scroll direction state
itemsCountnumberYes-Total number of items in the list
itemWidthnumberYes-Width of each item in pixels
gapnumberNo0Gap between items in pixels
paddingHorizontalnumberNo0Container horizontal padding
containerWidthnumberNo500Container width for auto-scroll calculations
onMove(id: string, from: number, to: number) => voidNo-Callback when item position changes
onDragStart(id: string, position: number) => voidNo-Callback when dragging starts
onDrop(id: string, position: number) => voidNo-Callback when dragging ends
onDragging(id: string, overItemId: string | null, xPosition: number) => voidNo-Callback during dragging

Return Value

UseHorizontalSortableReturn

PropertyTypeDescription
animatedStyleStyleProp<ViewStyle>Animated style for the sortable item
panGestureHandlerGestureTypePan gesture to pass to GestureDetector
registerHandle(registered: boolean) => voidCallback for handle registration
isMovingbooleanWhether the item is currently being dragged
hasHandlebooleanWhether the item has a drag handle component

HorizontalScrollDirection

enum HorizontalScrollDirection {
  None = "none",
  Left = "left",
  Right = "right",
}

Example

import { useHorizontalSortable } from "react-native-reanimated-dnd";

const { animatedStyle, panGestureHandler, isMoving } = useHorizontalSortable({
  id: item.id,
  positions,
  leftBound,
  autoScrollDirection,
  itemsCount: 5,
  itemWidth: 120,
  gap: 10,
  paddingHorizontal: 16,
  onMove: (id, from, to) => {
    console.log(`Item ${id} moved from ${from} to ${to}`);
  },
});

See Also