An image that comes from a URL or an API response has no size React Native can know ahead of time, so giving it one is your job. Once the frame has a size, resizeMode decides how the picture fills it, and a sized View lets you layer text or buttons on top of the picture.
Key terms
uri: the field of thesourceobject that holds the address of a network image, or adata:URI.data:URI: a URI scheme that carries encoded image data inline, such as image data returned by a REST API.resizeMode: theImageprop that decides how the image is resized when its frame doesn't match the image's raw dimensions.StyleSheet.absoluteFill: a style that setsposition: 'absolute'withleft,right,topandbottomall at 0.
Remote images need a size from you
A required file brings its dimensions with it. A network image doesn't, so you specify them manually:
tsx<Image source={{uri: product.photoUrl}} style={{width: 96, height: 96}} />
Don't leave an Image with only a uri and no size. If you prefer props to a style, Image also takes width and height props that set the component's width and height.
React Native asks for the size to keep the layout stable. A browser renders an unsized image as a 0x0 element, downloads it, then renders it again at its real size, and the page jumps each time an image arrives. React Native leaves that behavior out on purpose. It is more work to know a remote image's dimensions or aspect ratio in advance, but the layout doesn't move while images load.
When you don't know the dimensions, Image.getSize(uri) returns a Promise with the image's width and height in pixels before you display it. It can fail when the image isn't found or doesn't download, so handle the error. Once an image has loaded, the onLoad callback also receives its width and height in nativeEvent.source.
Encoded data follows the same rule. With the data: scheme in uri, you still set the width and height yourself, and data URIs are best kept for very small, dynamic images such as icons in a list that comes from a database. For real URLs, use https so the requests satisfy App Transport Security on iOS.
cover, contain and the other modes
When the frame's shape doesn't match the image, resizeMode decides what happens. It defaults to cover.
coverscales the image uniformly until both dimensions are equal to or larger than the frame, with at least one dimension matching it. Parts of the image can be clipped.containscales uniformly until both dimensions are equal to or smaller than the frame, so the whole image is visible.stretchscales width and height independently, which may change the aspect ratio.repeattiles the image across the frame at its own size, scaling it down only when it is larger than the view. Android supports it as well as iOS.centercenters the image and scales it down when it is larger than the frame. It never scales the image up.
The type definitions accept a sixth value, none, which doesn't resize the image and shows it at its intrinsic size.
You can set resizeMode as a prop or inside the style, and the two places don't carry equal weight. React Native reads objectFit from the style first, then the resizeMode prop, then resizeMode in the style, and falls back to cover. The objectFit style prop takes cover, contain, fill, scale-down and none, where fill maps to stretch, scale-down maps to contain and none maps to none. So when a shared style sets objectFit, a resizeMode prop on the same element has no effect.
Text on top of a photo
Image can't hold children. Give it some and it throws this error:
The <Image> component cannot contain children. If you want to render content on top of the image, consider using the <ImageBackground> component or absolute positioning.
ImageBackground is deprecated and will be removed in a future release, so take the second route. Render an absolutely positioned Image inside a View, followed by the content that should appear on top, and give that View a width and a height of its own. The TripHeader component below follows this pattern:
tsx<Image source={{uri: photoUrl}} style={StyleSheet.absoluteFill} /> <Text style={styles.title}>{title}</Text>
StyleSheet.absoluteFill pins all four edges of the photo to the header, so the network image takes its size from the header's width and height. The Text comes after the Image, which puts it on top. With the default cover, the photo fills the whole header and loses whatever doesn't fit.
Code Examples
import {Image, StyleSheet, Text, View} from 'react-native';
type TripHeaderProps = {
photoUrl: string;
title: string;
};
export default function TripHeader({photoUrl, title}: TripHeaderProps) {
return (
// The containing View sets the size that the photo fills
<View style={styles.header}>
{/* resizeMode defaults to cover: the photo fills the header and may be clipped */}
<Image source={{uri: photoUrl}} style={StyleSheet.absoluteFill} />
{/* Rendered after the Image, so it appears on top of the photo */}
<Text style={styles.title}>{title}</Text>
</View>
);
}
const styles = StyleSheet.create({
header: {
width: '100%',
height: 220,
justifyContent: 'flex-end',
padding: 16,
},
title: {
color: '#ffffff',
fontSize: 28,
fontWeight: '700',
},
});