Index of every page on this site: llms.txt
All skills
Run it yourself

Software Mansion's best practices for gestures in React Native apps using React Native Gesture Handler. Use when implementing tap, pan, pinch, rotation, swipe, long press, fling, hover, drag, or any touch interaction. Trigger on: 'gesture handler', 'GestureDetector', 'tap gesture', 'pan gesture', 'pinch gesture', 'rotation gesture', 'long press', 'fling', 'hover gesture', 'swipe', 'pinch to zoom', 'drag', 'touch handling', 'Pressable', 'RectButton', 'Swipeable', 'Touchable', 'DrawerLayout', 'VirtualGestureDetector', or any request to handle user touch input in a React Native app.

Use this Skill: https://skilld.dev/gh/software-mansion-labs/skills/gestures

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

testing.md

≈1.2k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Testing and Troubleshooting

Jest testing patterns and common issues for React Native Gesture Handler.

For full testing API, webfetch Testing Guide. For troubleshooting, webfetch Troubleshooting Guide.


Jest Setup

Add the RNGH mock to your Jest configuration:

{
  "jest": {
    "preset": "react-native",
    "setupFiles": ["./node_modules/react-native-gesture-handler/jestSetup.js"]
  }
}

Testing Gesture Callbacks

Use fireGestureHandler to simulate gesture event sequences:

import {
  fireGestureHandler,
  getByGestureTestId,
} from 'react-native-gesture-handler/jest-utils';
import { State } from 'react-native-gesture-handler';

// v3 -- test gesture hooks directly with renderHook
test('Pan gesture tracks translation', () => {
  const onActivate = jest.fn();
  const panGesture = renderHook(() =>
    usePanGesture({
      disableReanimated: true,
      onActivate: (e) => onActivate(e),
    })
  ).result.current;

  fireGestureHandler(panGesture, [
    { state: State.BEGAN },
    { state: State.ACTIVE, translationX: 50 },
    { translationX: 100 },
    { state: State.END },
  ]);

  expect(onActivate).toHaveBeenCalledTimes(1);
});

fireGestureHandler Auto-Fill Behavior

  • Missing BEGIN and END events are added automatically
  • oldState is filled from the previous event's state
  • Events after the first ACTIVE can omit state (defaults to ACTIVE)
  • If the first event has no state, ACTIVE is assumed
  • Handler-specific defaults (e.g., numberOfTouches, x, y) are populated

getByGestureTestId

Find gestures in rendered components by testID:

// In component
const tap = useTapGesture({ testID: 'my-tap', onDeactivate: handleTap });

// In test
const gesture = getByGestureTestId('my-tap');
fireGestureHandler(gesture, [{ state: State.ACTIVE }]);

The testID must be unique across all rendered gestures in the test.

Disable Reanimated in Tests

Set disableReanimated: true when testing gesture callbacks to ensure they run synchronously on the JS thread:

const pan = usePanGesture({ disableReanimated: true, onUpdate: handleUpdate });

Without this, callbacks run as worklets and may not execute during synchronous test assertions.


Common Issues

"Multiple instances of Gesture Handler were detected"

Dependencies have installed their own copy of RNGH instead of using the app's version.

Fix with npm:

npm ls react-native-gesture-handler
# Add to package.json:
# "overrides": { "react-native-gesture-handler": "<your-version>" }
npm install

Fix with yarn:

yarn why react-native-gesture-handler
# Add to package.json:
# "resolutions": { "react-native-gesture-handler": "<your-version>" }
yarn

Gestures Not Working

  1. Verify GestureHandlerRootView wraps the component tree
  2. Check that gestures and the component share the same root view
  3. For modals, add a separate GestureHandlerRootView inside the modal
  4. Import ScrollView/FlatList from react-native-gesture-handler

enabled Prop Does Not Take Effect Immediately

Changes to enabled only apply when a new gesture starts (finger touches screen). Setting enabled: false during an ongoing gesture will not cancel it. This is by design.

Android Gesture Cancellation in Third-Party Components

If gestures stop working inside third-party native components, use unstable_forceActive on GestureHandlerRootView:

<GestureHandlerRootView unstable_forceActive>
  {/* Prevents Android from canceling gestures */}
</GestureHandlerRootView>

This prevents Android native views from locking touch event delivery and canceling gesture recognition.

Native Gesture State Flow Deviations

The Native gesture (useNativeGesture / Gesture.Native()) may deviate from the standard state flow due to platform-specific accommodations. This is expected behavior when wrapping native touch-handling components.

On web, the Native gesture cannot block scrolling on a ScrollView due to platform API constraints.

Touchables Render Extra Views

Touchable components (TouchableOpacity, etc.) render two additional views. Style the outer view with style and the inner with containerStyle.

Web: Scroll Not Working with Custom Gestures

Add touchAction="pan-y" (or "pan-x" for horizontal scroll) to GestureDetector on web to allow browser scrolling alongside custom gesture handling.

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

1 warning2mo3 checks · Risk SAFE
  • Gen Agent Trust Hub2mo

    The skill provides comprehensive documentation and best practices for React Native Gesture Handler. It includes patterns for various gesture types, threading management, and migration guides. All external references are to official documentation, and the suggested commands are standard development practices.

  • Socket2mo

    No alerts

  • Snyk2mo

    Risk: MEDIUM · 1 issue

Signed by skilld at 43cde80. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 weeks ago.

Activeupdated 5 months ago

README badge

README badge for software-mansion-labs/skills/gestures