What if adding any picture to your app was as easy as pointing and clicking?
Why Image component (local and remote) in React Native? - Purpose & Use Cases
Imagine you want to show pictures in your app. You try to add images by writing long code for each picture, handling files manually, and downloading images yourself.
This manual way is slow and tricky. You might forget to load images correctly, or the app crashes if the image URL is wrong. Managing local files and remote images separately makes your code messy and hard to fix.
The Image component in React Native makes showing pictures easy. It handles both local files and online images smoothly with simple code. You just tell it where the image is, and it does the rest.
const img = require('./img.png'); // then write complex code to display or fetch images
<Image source={require('./img.png')} />
<Image source={{uri: 'https://example.com/pic.jpg'}} />You can quickly add beautiful pictures from your phone or the internet, making your app lively and engaging without headaches.
Think of a social media app where users upload photos. Using the Image component, the app shows local photos from the device and remote photos from friends instantly and reliably.
Manual image handling is slow and error-prone.
The Image component simplifies showing local and remote pictures.
This makes your app look great with less code and fewer bugs.
Practice
Image component?Solution
Step 1: Understand local image usage
Local images require therequire()function to load the file correctly in React Native.Step 2: Check the syntax for local images
The correct syntax usessource={require('path')}, noturiorsrcattributes.Final Answer:
<Image source={require('./assets/photo.png')} /> -> Option CQuick Check:
Local images use require() = C [OK]
- Using uri for local images
- Using src attribute instead of source
- Passing string directly without require()
Solution
Step 1: Identify remote image syntax
Remote images require an object with aurikey inside thesourceprop.Step 2: Verify correct object structure
The correct syntax issource={{ uri: 'URL' }}, not using require or src attributes.Final Answer:
<Image source={{ uri: 'https://example.com/image.png' }} /> -> Option DQuick Check:
Remote images use source={{ uri: URL }} = D [OK]
- Using require() for remote URLs
- Using src attribute instead of source
- Passing URL as a string directly to source
import React from 'react';
import { Image } from 'react-native';
export default function App() {
return (
<Image source={{ uri: 'https://example.com/pic.jpg' }} style={{ width: 100, height: 100 }} accessibilityLabel="Example image" />
);
}Solution
Step 1: Analyze the image source
The source uses a remote URL withuri, so it loads an internet image.Step 2: Check style and accessibility
The style sets width and height to 100, and accessibilityLabel is valid for screen readers.Final Answer:
A 100x100 image loaded from the internet with accessibility label -> Option AQuick Check:
Remote image with size and label = B [OK]
- Forgetting to set width and height style
- Thinking accessibilityLabel causes error
- Assuming remote images won't load
import React from 'react';
import { Image } from 'react-native';
export default function App() {
return (
<Image source="./assets/photo.png" style={{ width: 50, height: 50 }} />
);
}Solution
Step 1: Check source prop for local image
Local images must userequire()inside the source prop, not a string path.Step 2: Verify style and component usage
The style prop is correctly placed and numbers for width/height are valid in React Native.Final Answer:
The source prop should use require() for local images -> Option BQuick Check:
Local images need require() = A [OK]
- Passing string path directly to source
- Misplacing style prop
- Using strings for width/height instead of numbers
Solution
Step 1: Verify local and remote image sources
Local images must userequire(), remote images usesource={{ uri: URL }}.Step 2: Check layout and accessibility
UsingflexDirection: 'row'places images side by side, and accessibilityLabel is correctly added.Final Answer:
Code snippet with require() for local, uri for remote, row layout, and labels -> Option AQuick Check:
Local require + remote uri + row layout = A [OK]
- Using string path instead of require() for local images
- Using uri with local file paths
- Missing accessibility labels
- Incorrect flexDirection for horizontal layout
Ready to prove it?
Pass the test and this pattern moves unknown
