Skip to main content

Image

Shows an image. It renders a box (a <div>) with an <img> inside. The style sizes the box, and resizeMode fits the image in it.

import { Image } from 'react-native'

export function Avatar({ url }) {
return (
<Image
source={{ uri: url }}
style={{ width: 48, height: 48, borderRadius: 24 }}
accessibilityLabel="Profile photo"
/>
)
}

With no size in the style and no width and height in the source, the image is 0 × 0, as on native. The box clips the image to its border radius.

Sources​

sourceOn the web
{ uri }The src of the <img>. A width and height in the source size the box when the style doesn't.
An array with scale on each entryA srcset with x descriptors. The browser picks the entry for the pixel density of the screen.
An array with width on each entryA srcset with w descriptors. sizes is the style width, or 100vw when the style has none. The browser picks by layout width.
An array with mixed entriesThe first entry, as on Android.
{ uri, headers }, method, bodyA fetch with the headers, the method, and the body. The <img> shows the result.
A stringWeb only. A URL, for example the one that your bundler gives for an imported image file.
A numberA Metro asset id from require('./image.png'). See Local images.

The src and srcSet props work too, as on native.

Local images​

In a React Native app with no Expo, on @rootnative/web-metro, require('./logo.png') gives a number, as on native. Metro registers the image, and Image loads it from the URL that Metro gives:

  • A srcset lists the @2x and @3x files of the image, so the browser loads the file for the pixel density of the screen.
  • The box takes the size of the image when the style has none, as on native.
  • On the dev server, the URL asks for the .web.png file of the image when the image has one.

In an Expo app, require gives the URL and the size of the image, and Image reads them: see Expo. Vite gives no number for require. An id that Metro didn't register loads nothing, and the image warns once. With Vite, import the image and pass the URL:

import { Image } from 'react-native'

import logo from './logo.png'

export function Logo() {
return <Image source={logo} style={{ width: 120, height: 40 }} />
}

Props​

PropOn the web
resizeModecover (the default), contain, stretch, center, repeat, and none. center shrinks an image that is bigger than the box, and keeps a smaller one at its own size. none keeps the image size and puts it in the top left corner, as on iOS. resizeMode and objectFit in the style work too.
tintColorPaints the color through the shape of the image. The box then carries role="img" and the label.
blurRadiusA CSS blur.
defaultSourceShows until the source loads. It takes a URL, { uri }, or a Metro asset id.
alt, accessibilityLabelThe text alternative of the image.
onLoadStart, onLoad, onLoadEnd, onErrorAs on native. onLoad gets nativeEvent.source with the uri, width, and height.
onProgressNever fires. The browser doesn't report progress for an image.
crossOrigin, referrerPolicyPass to the <img>.
width, heightSize the box when the style doesn't.
loading, fetchPriority, decodingWeb only. The loading hints of the <img>.
onLayout, accessibility props, nativeID, testIDAs on View.
onStartShouldSetResponder, onResponderGrant, and the other responder propsAs on View.
resizeMethod, fadeDuration, progressiveRenderingEnabled, loadingIndicatorSourceNative only. Accepted and ignored.

With resizeMode="repeat" or a tintColor, the box paints the image as a CSS background or mask. The <img> stays in the box, hidden, so that the load events still fire. With a tintColor, center doesn't shrink a large image.

An image that fails to load leaves the box empty, as on native. The <img> hides, so the browser doesn't draw its broken-image icon or the alt text, and the box carries role="img" and the label. A new source shows the image again. The same applies while a source with headers loads. In server HTML, a failed image shows the browser's icon until the page hydrates.

Images in server HTML​

With a static export or a server render, the <img> is in the HTML. The browser requests the image before your JavaScript loads, and React adds a preload link for it to the <head>. That's good for the largest image of the first screen, the Largest Contentful Paint (LCP) element. On a slow network, the other images share the bandwidth with your JavaScript, so the page becomes interactive later.

Give each image the hint that fits it:

ImageProps
The LCP image, visible before your JavaScript runsfetchPriority="high". Never loading="lazy".
Far below the first screenloading="lazy". React adds no preload link, and the browser loads it when it's close.
Hidden until your JavaScript runs, for example by an entrance fadeNo hint. A high priority takes bandwidth from the JavaScript that shows the image.
<Image source={{ uri: hero }} fetchPriority="high" style={styles.hero} />
<Image source={{ uri: logo }} loading="lazy" style={styles.logo} />

A browser starts to load a lazy image before it scrolls into view, often more than a screen away. So loading="lazy" delays only an image far down the page. It drops the preload link in each case.

In an Expo app, the react-native types don't declare these props. See Web-only props in TypeScript.

Statics​

MethodOn the web
Image.getSize(uri)Loads the image and gives its size. It returns a promise, or calls the success and failure callbacks.
Image.getSizeWithHeaders(uri, headers)The same, with a fetch that sends the headers.
Image.prefetch(url)Loads the image into the browser cache. Resolves true.
Image.prefetchWithMetadata(url, queryRootName)The same as prefetch, as on Android. The other arguments are for native logs.
Image.queryCache(urls)Resolves {}. The browser doesn't show its cache to a page.
Image.resolveAssetSource(source){ uri, width, height, scale } of the source. For a Metro asset id, the file for the screen, as on native. null for an id that Metro didn't register.

getSize needs a browser. On the server it rejects.

Ref​

The ref is the box element. It has the measure methods of View. The type is ImageInstance.