Skip to main content
useAnimatedValueXY is a React hook that creates a persistent Animated.ValueXY instance for animating 2D positions. The animated value persists across component re-renders, making it ideal for drag-and-drop, pan gestures, and position-based animations.

Signature

Parameters

  • initialValue - Object containing initial x and y coordinates
    • x - The initial x coordinate value
    • y - The initial y coordinate value
  • config (optional) - Configuration options for the animated value
    • useNativeDriver?: boolean - Whether to use the native driver for animations

Returns

An Animated.ValueXY instance that persists across re-renders.

Usage

Common Patterns

Drag and Drop

Swipe to Dismiss

Parallax Scrolling

Floating Action Button

Multi-touch Drag

ValueXY Methods

Animated.ValueXY provides several useful methods:

getLayout()

Convert to {left, top} for absolute positioning:

getTranslateTransform()

Convert to [{translateX}, {translateY}] for transforms:

setValue()

Directly set the value:

setOffset()

Set an offset to be added to the value:

flattenOffset()

Merge offset into the base value:

extractOffset()

Set offset to current value and reset value to zero:

When to Use

Use useAnimatedValueXY when you need:
  • To animate 2D positions (x, y coordinates)
  • Drag-and-drop functionality
  • Pan gesture handling
  • Parallax scrolling effects
  • Swipeable components

Alternatives

  • useAnimatedValue - For single numeric values (opacity, scale, rotation)
  • react-native-gesture-handler - More performant gesture handling
  • react-native-reanimated - Better performance for complex animations

Implementation Details

This hook:
  • Uses useRef to store the Animated.ValueXY instance
  • Creates the animated value only once on first render
  • Returns the same instance on subsequent re-renders
  • The initial value is only used during the first render

Performance Tips

  • Native Driver Limitation: useNativeDriver: false is required for layout animations
  • ScrollView: Use scrollEventThrottle={16} for smooth 60fps scrolling
  • PanResponder: Consider react-native-gesture-handler for better performance
  • Complex Animations: Use react-native-reanimated for animations running on the UI thread

See Also