For AI agents: the complete documentation index is available at /2.x/llms.txt, the full documentation bundle is available at /2.x/llms-full.txt, and this page is available as Markdown at /2.x/guide/usage/basic-usage.md.
v3 is stable. See what changed
  • English
  • 2.x
  • Basic Usage

    GestureViewer

    import { useCallback, useState } from 'react';
    import { ScrollView, Image, Modal } from 'react-native';
    import { GestureViewer } from 'react-native-gesture-image-viewer';
    
    function App() {
      const images = [...];
      const [visible, setVisible] = useState(false);
    
      const renderImage = useCallback((imageUrl: string) => {
        return <Image source={{ uri: imageUrl }} style={{ width: '100%', height: '100%' }} resizeMode="contain" />;
      }, []);
    
      return (
        <Modal visible={visible} onRequestClose={() => setVisible(false)}>
          <GestureViewer
            data={images}
            renderItem={renderImage}
            ListComponent={ScrollView}
            onDismiss={() => setVisible(false)}
          />
        </Modal>
      );
    }
    Accurate bounds for

    contain content For accurate zoom and pan bounds, provide natural content dimensions with getItemDimensions, or report them after loading with setItemDimensions. See the item dimensions guide.

    useGestureViewerController

    You can programmatically control the GestureViewer using the useGestureViewerController hook.

    import { GestureViewer, useGestureViewerController } from 'react-native-gesture-image-viewer';
    
    function App() {
      const { goToIndex, goToPrevious, goToNext, zoomIn, zoomOut, resetZoom, rotate } =
        useGestureViewerController();
    
      return (
        <View>
          <GestureViewer data={images} renderItem={renderImage} />
          <View>
            <Button title="Prev" onPress={goToPrevious} />
            <Button title="Jump to index 2" onPress={() => goToIndex(2)} />
            <Button title="Next" onPress={goToNext} />
          </View>
        </View>
      );
    }

    useGestureViewerState

    You can get the current state of the GestureViewer using the useGestureViewerState hook.

    import { GestureViewer, useGestureViewerState } from 'react-native-gesture-image-viewer';
    
    function App() {
      const { currentIndex, totalCount } = useGestureViewerState();
    
      return (
        <View>
          <GestureViewer data={images} renderItem={renderImage} />
          <View>
            <Text>{`${currentIndex + 1} / ${totalCount}`}</Text>
          </View>
        </View>
      );
    }

    useGestureViewerEvent

    You can subscribe to specific events from GestureViewer using the useGestureViewerEvent hook. This allows you to respond to real-time gesture changes like zoom and rotation.

    import { GestureViewer, useGestureViewerEvent } from 'react-native-gesture-image-viewer';
    
    function App() {
      useGestureViewerEvent('zoomChange', (data) => {
        console.log(`Zoom changed from ${data.previousScale} to ${data.scale}`);
      });
    
      useGestureViewerEvent('rotationChange', (data) => {
        console.log(`Rotation changed from ${data.previousRotation}° to ${data.rotation}°`);
      });
    
      useGestureViewerEvent('tap', (data) => {
        if (data.kind === 'single') {
          console.log(`Tapped item ${data.index} at (${data.x}, ${data.y})`);
        }
      });
    
      return <GestureViewer data={images} renderItem={renderImage} />;
    }

    GestureTrigger

    GestureTrigger supports smooth trigger-based animations that create seamless transitions from trigger elements (like thumbnails) to the full modal view. This feature enhances user experience by maintaining visual continuity between the trigger and modal content.

    import { GestureTrigger, GestureViewer } from 'react-native-gesture-image-viewer';
    
    function App() {
      const [visible, setVisible] = useState(false);
    
      return (
        <View>
          <GestureTrigger onPress={() => setVisible(true)}>
            <Pressable>
              <Image source={{ uri }} />
            </Pressable>
          </GestureTrigger>
          <GestureViewer data={images} renderItem={renderImage} onDismiss={() => setVisible(false)} />
        </View>
      );
    }