Images can make apps slow if they are too big. Optimizing images helps apps load faster and use less data.
Image optimization in React Native
import { Image } from 'react-native'; <Image source={{ uri: 'https://example.com/image.jpg' }} style={{ width: 100, height: 100 }} resizeMode="contain" loadingIndicatorSource={require('./loading.png')} />
source sets the image location.
resizeMode controls how the image fits inside the space.
import { Image } from 'react-native'; <Image source={{ uri: 'https://example.com/photo.png' }} style={{ width: 200, height: 200 }} resizeMode="cover" />
import { Image } from 'react-native'; <Image source={require('./local-image.jpg')} style={{ width: 150, height: 150 }} resizeMode="contain" />
import { Image } from 'react-native'; <Image source={{ uri: 'https://example.com/image.webp' }} style={{ width: 100, height: 100 }} resizeMode="center" />
This app shows a small React Native logo optimized to fit nicely inside a 100x100 box without distortion.
import React from 'react'; import { View, Image, StyleSheet, Text } from 'react-native'; export default function App() { return ( <View style={styles.container}> <Text style={styles.title}>Optimized Image Example</Text> <Image source={{ uri: 'https://reactnative.dev/img/tiny_logo.png' }} style={styles.image} resizeMode="contain" /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#fff' }, title: { fontSize: 18, marginBottom: 20 }, image: { width: 100, height: 100 } });
Use resizeMode to control how images fit their space: contain keeps the whole image visible, cover fills the space but may crop.
Use smaller image files or modern formats like WebP to reduce size and load time.
Always set explicit width and height to help the app layout images efficiently.
Optimizing images helps your app run faster and use less data.
Use resizeMode and proper image sizes to control how images appear.
Choose the right image format and size before adding images to your app.
Practice
Solution
Step 1: Understand image optimization purpose
Optimizing images reduces their size and improves loading speed.Step 2: Identify the benefit in app performance
Smaller images use less data and make the app faster to load.Final Answer:
It makes the app load faster and use less data -> Option DQuick Check:
Image optimization = faster app and less data [OK]
- Thinking optimization changes colors
- Believing optimization adds animations
- Assuming optimization increases file size
Solution
Step 1: Recall correct prop name for resizing
The correct prop to control image resizing isresizeMode.Step 2: Check valid resizeMode values
Valid values include 'cover', 'contain', 'stretch', but not 'zoom'.Final Answer:
<Image source={uri} resizeMode='cover' /> -> Option BQuick Check:
resizeMode='cover' is correct syntax [OK]
- Using 'resize' instead of 'resizeMode'
- Using invalid values like 'zoom'
- Using 'mode' prop which does not exist
Solution
Step 1: Understand impact of large images
Large images consume more memory and take longer to load.Step 2: Identify app behavior with large images
This can slow down the app but usually does not crash it immediately.Final Answer:
The app may become slow and use more memory -> Option AQuick Check:
Large images = slower app and more memory use [OK]
- Thinking large images load faster
- Assuming images auto resize always
- Believing app crashes immediately
<Image source={{uri: 'image.png'}} resizeMode='stretch' />What is the likely mistake?
Solution
Step 1: Check image resizing requirements
In React Native, images need explicit width and height styles to resize properly.Step 2: Verify code props and values
The source and resizeMode are correct; missing styles cause no resize effect.Final Answer:
The image style is missing width and height -> Option CQuick Check:
Missing width/height stops resizing [OK]
- Thinking resizeMode 'stretch' is invalid
- Assuming source must be string only
- Ignoring need for width and height styles
Solution
Step 1: Understand responsive image needs
Different screen sizes need different image resolutions for best quality and performance.Step 2: Choose best method to handle multiple sizes
Using multiple image versions and selecting them withrequire()based on screen size is efficient.Final Answer:
Use multiple image versions and select with require() based on screen size -> Option AQuick Check:
Multiple versions + require() = best optimization [OK]
- Using one large image wastes memory
- Ignoring resizing causes poor quality or slow app
- Assuming SVG works for all images
Ready to prove it?
Pass the test and this pattern moves unknown
