react native use variable for image file
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In React Native, using a variable for an image source depends on whether the image is bundled locally or loaded remotely. Remote URLs can be passed through variables directly. Local bundled images are different: require must be statically analyzable, so you cannot build the file path dynamically at runtime. That distinction is the key to solving most image-source questions in React Native.
Remote Images Can Use Variables Directly
If the source is a URL, just store it in a variable and pass it through the uri form.
This is fully dynamic. You can swap the URL based on props, state, or API data.
Local Images Cannot Use Dynamic require
This does not work:
React Native’s bundler needs to know local assets at build time. A runtime-computed path cannot be resolved the way a normal JavaScript string can.
That is why developers often see confusing errors when trying to treat local assets like ordinary strings.
Use a Lookup Object for Bundled Assets
The usual fix is to map keys to static require calls.
This keeps the choice dynamic while leaving each require static enough for the bundler.
Choose Images from Props or State
This pattern works naturally with props and state.
The fallback matters because an unknown key would otherwise produce undefined, which breaks rendering.
Use Variables with Remote or Packaged Sources Correctly
A practical rule:
- remote image:
source={{ uri: variable }} - local bundled image:
source={lookup[key]}
Trying to unify both into one naive string-based approach is where most bugs come from.
If you need both styles in one component, branch deliberately:
That makes the source type explicit.
Why React Native Works This Way
Bundled assets are processed during the app build so the platform can package and reference them efficiently. For that to work, Metro needs to see the asset references statically.
Remote URLs are different because they are not bundled into the app. They are resolved at runtime over the network, so a normal variable is fine.
Once you understand that build-time versus runtime difference, the API behavior makes much more sense.
Common Pitfalls
The most common mistake is attempting require(variable) for local image files. That will not work because the bundler cannot inspect the final path dynamically.
Another issue is forgetting to set width and height on the Image style. Even a correct source may appear invisible without dimensions.
Developers also sometimes assume remote and local sources use the same structure. They do not. Remote images use the uri object form, while local bundled assets use the result of require.
Finally, when using lookup objects, always handle missing keys so the UI does not crash on unexpected input.
Summary
- Remote image URLs can be stored in variables and used with
source={{ uri: ... }}. - Local bundled images cannot use dynamic
require. - For local assets, map keys to static
requirecalls and select from that object. - Treat remote and local sources as two different source types.
- Add dimensions and sensible fallbacks to make image rendering reliable.
Related reading
- React React-router async components mounted multiple times componentDidMount called many times
- react router doesn't work in aws s3 bucket
- React Router DOM not working correctly on Amplify Console AWS
- React setState takes 200ms
- ReactiveCocoa vs RxSwift - pros and cons?
- React/RCTBridgeModule.h file not found
- React, setState with async updater parameter?
- Read environment variables in Node.js
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.