
Product
Introducing Socket Fix for Safe, Automated Dependency Upgrades
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
expo-image-viewer
Advanced tools
An image viewer using Expo Image, Reanimated, and React Native Gesture Handler
This is a simple image viewer built with React Native Reanimated and React Native Gesture Handler. Images use Expo Image, which is highly performant and allows for the prefetching of images to display in the image viewer.
This package is fully compatible with Expo Go.
yarn add expo-image-viewer
First, you will want to wrap your app inside a <GestureHandlerRootView>
component. You should place this somewhere
near the root of your app, such as in your App.tsx
file.
import { GestureHandlerRootView } from 'react-native-gesture-handler';
export default function App() {
return (
<GestureHandlerRootView>
<App />
</GestureHandlerRootView>
);
}
Next, you should additionally wrap your app inside of ImageViewerProvider
.
import { ImageViewerProvider } from 'expo-image-viewer';
export default function App() {
return (
<GestureHandlerRootView>
<ImageViewerProvider>
<App />
</ImageViewerProvider>
</GestureHandlerRootView>
);
}
Finally, you can use the ViewerImage
component to display images in the image viewer.
import { ViewerImage } from 'expo-image-viewer';
export default function MyScreen() {
return (
<View style={{flex: 1}}>
<ViewerImage
source="https://images.unsplash.com/photo-1634179127707-4b8b9b5b2b0b?ixid=MnwxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHwzNnx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60"
initialDimensions={{
height: 500,
width: 500
}}
heightModifier={0.6}
title="My Image"
/>
</View>
);
}
Whenever the ViewerImage
component is pressed, the image viewer will open and display the image.
Prop | Type | Description |
---|---|---|
children | React.ReactNode | The children to render inside of the provider. |
options | ImageViewerOptions | The options to use for the image viewer. |
Option | Required | Default | Type | Description |
---|---|---|---|---|
showHeader | No | true | boolean | Show the header. |
showFooter | No | true | boolean | Show the footer. |
showCloseButton | No | true | boolean | Show the close button. |
preloadImages | No | true | boolean | When supplying an array of images, the each of the images will be preloaded. See more below. |
heightModifier | No | 1 | number | A number between 0 and 1 that defines the modifier for the height. Useful if displaying images in a feed and want to maintain aspect ratio without having images disappear off the screen |
swipeToDismiss | No | true | boolean | Whether to allow the user to swipe to dismiss the viewer. |
FooterComponent | No | null | () => React.JSX.Element | A footer component to render. Background color should be transparent. The animated displaying/hideing of this will be handled for you. |
ActivityIndicatorComponent | No | null | () => React.JSX.Element | A component to render for an activity indicator. It will automatically be positioned in the center of your initial dimensions. |
Prop | Required | Default | Type | Description |
---|---|---|---|---|
source | Yes | NA | string string[] | The source of the image. |
contentFit | No | contain | ImageContentFit (see Expo Image) | How the image should be displayed within its bounds. |
title | No | NA | string | The title of the image. Will be shown in the image viewer if the header is enabled. |
heightModifier | No | NA | number | Overrides the heightModifier provided to the ImageViewerProvider . |
blurRadius | No | NA | number | A blur radius to apply to the image (see Expo Image) |
recyclingKey | No | NA | string | A recycling key to be used with a recycler view such as FlashList (see Expo Image) |
initialDimensions | Yes | {height: 300, width: 300}` | `{height: number, width: number} | A height and width that should be the default for the image. |
onLoad | No | NA | (e: ImageLoadEventData) => void | A function to run when the onLoad event is called. |
onPress | No | NA | () => unknown | A function to run when the image is pressed |
useInitialDimensions | No | false | boolean | Whether to keep the initial dimensions of the image even after the load. |
showActivityIndicator | No | true | boolean | Whether to show the activity indicator while the image is loading. You can supply your own component to display, otherwise will use the React Native ActivityIndicator component. |
activityIndicatorSize | No | large | small | large | Size of the activity indicator |
activityIndicatorColor | No | white | ColorValue | Color of the activity indicator |
If you need to get any of the current values (such as the current index of the displayed image or the image source) or
if you wish to create your own implementation of ViewerImage
, you can use the useImageViewer
hook.
Property | Type | Description |
---|---|---|
options | ImageViewerOptions | The options passed to the ImageViewerProvider . These are only provided for reference. They should be changed in the ImageViewerProvider . |
props | IImageViewerState | The current state of the image viewer. See below. |
dispatch | (action: React.Dispatch<IAction>) => void | A dispatch function to dispatch actions to the image viewer. See below. |
Property | Type | Description |
---|---|---|
params | IImageViewerParams | The params passed to the image viewer. See below. |
visible | boolean | Whether the image viewer is visible. |
viewerOpacity | Animated.SharedValue<number> | The opacity of the image viewer. Do not change this unless you are creating your own implementation. |
viewerRef | React.RefObject<View> | null | The ref of the image's container. For example, the pressable that you are wrapping your <Image> in. Used to perform a measurement of the view when opening. Do not change this value unless you are creating your own implementation. |
Property | Type | Description |
---|---|---|
source | string string[] | The source of the image. |
title | string | The title of the image. |
index | number | The index of the image being displayed in the viewer. |
IAction
is an object that takes a type
and a payload
. These are the possible actions.
Action | Payload Type | Description |
---|---|---|
setState | IImageViewerState | Sets the state of the image viewer. |
setVisible | boolean | Sets the visibility of the image viewer. |
setIndex | number | Sets the index of the image to display in the viewer. |
FAQs
An image viewer using Expo Image, Reanimated, and React Native Gesture Handler
The npm package expo-image-viewer receives a total of 0 weekly downloads. As such, expo-image-viewer popularity was classified as not popular.
We found that expo-image-viewer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
Security News
CISA denies CVE funding issues amid backlash over a new CVE foundation formed by board members, raising concerns about transparency and program governance.
Product
We’re excited to announce a powerful new capability in Socket: historical data and enhanced analytics.