useDroppable

useDroppable is a custom React Hook that allows a component to become a droppable target for drag-and-drop operations. This hook manages the registration, layout, and state of a droppable component.

Minimal Example

// src/components/DroppableComponent.tsx
import { useDroppable } from "@mgcrea/react-native-dnd";

const DroppableComponent = ({ id, data, disabled }) => {
  const { setNodeRef, setNodeLayout, activeId, draggableState } = useDroppable({
    id,
    data,
    disabled,
  });

  return (
    <Animated.View ref={setNodeRef} onLayout={setNodeLayout}>
      {/* Render component content */}
    </Animated.View>
  );
};

Advanced Usage

// src/components/MyDroppable.tsx
import { type AnimateProps } from "@mgcrea/react-native-dnd";
import { type FunctionComponent, type PropsWithChildren } from "react";
import { type ViewProps } from "react-native";
import Animated, { useAnimatedStyle } from "react-native-reanimated";
import { type UseDraggableOptions } from "../hooks";

export type MyDroppableProps = AnimateProps<ViewProps> & UseDraggableOptions;

export const MyDroppable: FunctionComponent<PropsWithChildren<MyDroppableProps>> = ({
  children,
  id,
  disabled,
  data,
  style,
  ...otherProps
}) => {
  const { setNodeRef, setNodeLayout, activeId } = useDroppable({
    id,
    disabled,
    data,
  });

  const animatedStyle = useAnimatedStyle(() => {
    const isActive = activeId.value === id;
    const style = {
      opacity: isActive ? 0.9 : 1,
    };
    if (animatedStyleWorklet) {
      animatedStyleWorklet(style, isActive);
    }
    return style;
  }, [id]);

  return (
    <Animated.View ref={setNodeRef} onLayout={setNodeLayout} style={[style, animatedStyle]} {...otherProps}>
      {children}
    </Animated.View>
  );
};

Parameters

useDroppable accepts an object with the following properties:

  • Name
    id
    Type
    :UniqueIdentifier
    Description

    A unique identifier for the droppable component.

  • Name
    data
    Type
    :Data?
    = {}
    Description

    An optional data object that can be used to store additional information about the droppable component. Default is an empty object ({}).

  • Name
    disabled
    Type
    :boolean?
    = false
    Description

    A boolean indicating whether the droppable component should be disabled (i.e., not accept any draggable components). Default is false.

Interface

useDroppable returns an object with the following properties:

  • Name
    setNodeRef
    Type
    :RefCallback<Animated.View>
    Description

    A ref callback function used to store a reference to the droppable component’s Animated.View.

  • Name
    setNodeLayout
    Type
    :ViewProps["onLayout"]"
    Description

    An onLayout event handler that should be attached to the droppable component’s Animated.View to track its layout.

  • Name
    activeId
    Type
    :SharedValue<UniqueIdentifier | null>
    Description

    A SharedValue containing the ID of the currently active droppable component or null if there’s no active droppable component.

  • Name
    draggableState
    Type
    :SharedValue<DraggableState>
    Description

    A SharedValue containing the state of the currently dragged component.