React Native / Native descriptions and local interaction / Lesson 7
Native images and loading states
Render local and remote images with explicit dimensions, accessible meaning and recoverable failures.
On this page
Lesson goals
- Distinguish bundled and remote image sources.
- Provide dimensions and appropriate accessibility text.
- Handle remote failures without inventing a successful load.
Choose an image contract
An image can be bundled with an application or fetched from a remote source. Decide whether it conveys information or is decorative, how much space it needs and what the user sees when loading fails. A remote URL is an external dependency, not a guarantee that an image is available. Validate allowed destinations and avoid embedding credentials in a public image URL.
Use a bundled image
This component refers to a local asset by a static require and gives it explicit dimensions and an accessible label. The host project must supply the actual file at the shown path; no asset is included in this lesson. Avoid repeating the same label in production if adjacent text already provides equivalent information to assistive technology.
import { Image, Text, View } from "react-native";
export function WorkshopImage() {
return <View><Text>Workshop diagram</Text><Image source={require("./assets/workshop.png")} style={{ width: 160, height: 100 }} accessibilityLabel="Workshop diagram" /></View>;
}
A configured host displays the supplied workshop asset.
The native Image uses a bundled asset and a bounded display size.
Handle a remote failure
The second component receives a URI and displays a fallback if the native image reports an error. It is a simplified example and must be remounted or reset when a new URI represents a new attempt. It does not provide a real image server or claim a successful network request. Test slow, failed and successful loads on each supported target.
import { Image, Text, View } from "react-native";
import { useState } from "react";
export function RemoteImage({ uri }) {
const [failed, setFailed] = useState(false);
if (failed) return <Text>Image unavailable.</Text>;
return <View><Image source={{ uri }} style={{ width: 160, height: 100 }} accessibilityLabel="Workshop diagram" onError={() => setFailed(true)} /></View>;
}
A reported image error displays Image unavailable.
The component displays a text fallback after onError; the caller supplies an allowed URI.
Check semantics and layout
Choose a meaningful accessibility label for informational content, or avoid redundant announcements for decorative content according to the platform and component design. Set dimensions that fit available space and test long adjacent text and larger font settings. A label cannot communicate essential details that are missing from the image description, and a fixed sample size is not a responsive layout policy.
Verify on the target
Supply a synthetic local asset and a controlled remote endpoint in an isolated host, then test loading, failure and a new image attempt. Check the resulting accessibility tree and screen-reader experience on the intended platform. Record the build and target used. A JavaScript-only test can verify a fallback branch but cannot prove native image decoding, network behavior or announcement quality.
Common errors and fixes
The bundled asset cannot be resolved.
- Why it happens
- The file is absent or the static path is incorrect.
- How to fix it
- Add the reviewed asset at the expected project path.
- Prevent it next time
- Build the actual native host with its assets.
A failed image leaves an unexplained blank area.
- Why it happens
- The error path has no user-visible fallback.
- How to fix it
- Display a meaningful fallback and provide a retry when appropriate.
- Prevent it next time
- Test a controlled load failure.
A new URI still displays the old failure.
- Why it happens
- Failure state was not reset for a new attempt.
- How to fix it
- Reset or remount the image owner for the new source.
- Prevent it next time
- Test source changes after a failed load.
2 practice exercises
1. Why does the local example need an actual project asset?
Show worked solution
The static require is resolved by the native project’s bundler. The lesson supplies only a path, so the host must include a suitable file before a target build can display it.
2. Does onError prove every possible loading problem is handled?
Show worked solution
No. It illustrates one reported failure path. Test source changes, unavailable network, target-specific decoding and accessible fallback in the actual host.
Key takeaways
- Treat remote images as external dependencies.
- Provide appropriate meaning, dimensions and failure feedback.
- Verify image behavior on the native target.