This is covered in the documentation under the section "Static Resources":

The only allowed way to refer to an image in the bundle is to literally write require('image!name-of-the-asset') in the source.

// GOOD
<Image source={require('image!my-icon')} />

// BAD
var icon = this.props.active ? 'my-icon-active' : 'my-icon-inactive';
<Image source={require('image!' + icon)} />

// GOOD
var icon = this.props.active ? require('image!my-icon-active') : require('image!my-icon-inactive');
<Image source={icon} />

However you also need to remember to add your images to an xcassets bundle in your app in Xcode, though it seems from your comment you've done that already.

http://facebook.github.io/react-native/docs/image.html#adding-static-resources-to-your-app-using-images-xcassets

Answer from Colin Ramsay on Stack Overflow
Top answer
1 of 16
109

This is covered in the documentation under the section "Static Resources":

The only allowed way to refer to an image in the bundle is to literally write require('image!name-of-the-asset') in the source.

// GOOD
<Image source={require('image!my-icon')} />

// BAD
var icon = this.props.active ? 'my-icon-active' : 'my-icon-inactive';
<Image source={require('image!' + icon)} />

// GOOD
var icon = this.props.active ? require('image!my-icon-active') : require('image!my-icon-inactive');
<Image source={icon} />

However you also need to remember to add your images to an xcassets bundle in your app in Xcode, though it seems from your comment you've done that already.

http://facebook.github.io/react-native/docs/image.html#adding-static-resources-to-your-app-using-images-xcassets

2 of 16
97

RELEVANT IF YOU HAVE KNOWN IMAGES (URLS):

The way I hacked my way through this problem:

I created a file with an object that stored the image and the name of the image:

export const ANIMAL_IMAGES = {
  dog: {
    imgName: 'Dog', 
    uri: require('path/to/local/image')
  },
  cat: {
    imgName: 'Cat on a Boat', 
    uri: require('path/to/local/image')
  }
}

Then I imported the object into the component where I want to use it and just do my conditional rendering like so:

import { ANIMAL_IMAGES } from 'path/to/images/object';

let imgSource = null;

if (condition === 'cat') {
  imgSource = ANIMAL_IMAGES.cat.uri;
}

<Image source={imgSource} />

I know it is not the most efficient way but it is definitely a workaround.

Hope it helps!

🌐
React Native
reactnative.dev › docs › images
Images · React Native
1 week ago - React Native provides a unified way of managing images and other media assets in your Android and iOS apps. To add a static image to your app, place it somewhere in your source code tree and reference it like this:
Discussions

javascript - React Native - Dynamic Image Source
I'm trying to loop through the SOURCE array with the map method, but I keep getting this error: Unknown named module: '../images/one.jpeg' Anyone know why this is happening? The file path in the More on stackoverflow.com
🌐 stackoverflow.com
How do I dynamically change image sources?
I think you just... Change the image uri or url (?) I mean, you can also have a React component state or hook state that contains the image source and you just change that value during the component lifetime More on reddit.com
🌐 r/reactnative
4
3
September 1, 2021
Dynamic Image Sources of Local Image Files
Issue Description Unable to display images from dynamic sources. I have tried using require, which isn't meant to work as I found in searching for how to resolve this. What I would like is to h... More on github.com
🌐 github.com
3
October 7, 2016
Dynamic path using require in RN Image component
Use source={{uri: 'path/to/image'}}. Your issue looks like this one https://www.reddit.com/r/reactnative/comments/1asn0ek/why_are_my_remote_image_sources_working_but_not/ More on reddit.com
🌐 r/reactnative
27
1
February 29, 2024
🌐
Code Buckets
codebuckets.com › 2022 › 03 › 12 › dynamic-images-in-react-native
Working with Dynamic Images in React Native – Code Buckets
October 8, 2022 - In every other programming framework I’ve worked with, the obvious thing to do is to construct the image name and path dynamically and load it into the UI. In React Native this would be · const backgroundImage = require(`background${imageNumber}.jpg`); return ( <ImageBackground source={backgroundImage} style={styles.container}> {children} </ImageBackground> );
🌐
DEV Community
dev.to › kevilondo › display-images-dynamically-from-a-variable-in-react-native-524g
Display images dynamically (from a variable) in React Native - DEV Community
October 24, 2023 - const my_variable = require('../assets/image.jpg'); <Image source={my_variable} /> Doing this will definitely solve your problem and If you have any question regarding React Native or this specific post, feel free to ask, who knows, maybe other developers are going through the same problem and discussing certain issues here might help a lot of people.
🌐
YouTube
youtube.com › luke chaffey
React Native - Dynamic Image Source - YouTube
javascript: React Native - Dynamic Image SourceThanks for taking the time to learn more. In this video I'll go through your question, provide various answers...
Published   November 4, 2022
Views   636
🌐
Medium
everaldev.medium.com › how-to-load-local-dynamic-images-with-html-in-react-native-ed4d50c161d1
How to load local dynamic images in html in React Native | by Everaldo Junior | Medium
January 19, 2021 - “-If he is writing a post on Medium its because there’s is a way to import dynamic imports inside require, right?” ... So, i’m creating an object with all my images and it’s requires as properties of that object. const imagesList = { dwight: require('./src/assets/dwight.jpg'), michael: require('./src/assets/michael.jpg) } Now i can finally pass the image that we want to render in the source property in our custom <img/> tag.
Find elsewhere
🌐
Reactnativecode
reactnativecode.com › home › react native change image source dynamically using state on button click
React Native Change Image Source Dynamically using State on Button Click
March 1, 2018 - source = {require(‘/Users/benjilightstone/Coding/DroppinBoyss/Images/Map500Cut.png’)} ... Benj read our this tutorial this would help you : https://reactnativecode.com/get-image-from-local-resource/ 🙂 .
🌐
DEV Community
dev.to › juniorklawa › how-to-load-local-dynamic-images-with-html-in-react-native-3ea4
How to load local dynamic images with html in React Native - DEV Community
July 2, 2021 - But the main problem isn’t solved, our goal is to render local images dynamically using that custom tag that we’ve created. ... I added a source prop with the image path in <img> tag. ... const htmlContent = ` <h1>This HTML snippet is now rendered with native components !</h1> <img source="./src/assets/michael.jpg"/> <h2>Enjoy a webview-free and blazing fast application</h2> <em>Some random sentence!</em> `; <HTML renderers={{ img: (attribs) => { const imagePath = attribs.source; console.log(imagePath); return ( <Image key={attribs.source} style={styles.imageContainer} source={require('./src/assets/michael.jpg')} /> ); }, }} source={{html: htmlContent}} contentWidth={contentWidth} />
🌐
Nkaushik
nkaushik.com › react-native › dynamic-image-source-in-react-native
How to add dynamic image source in react native | N Kaushik
July 27, 2020 - In react native, we can add one local image to an Image component using a source props : <Image source={require("./images/logo.png")} /> But, if we want to add the image path dynamically, it doesn’t work : <Image source={require(`./images/${props.image}.png`)} /> It will throw one error.
🌐
JavaScript in Plain English
javascript.plainenglish.io › using-images-in-react-native-668e3a835858
Using Images in React Native. A few hours ago, I was working on a… | by Allan Graves | JavaScript in Plain English
August 8, 2024 - Require itself returns an Object, which is used as the source for the image. The Object in this case is an integer which Metro resolves at Image load time into a location. Okay, that one was easy. It’s not very dynamic though.
🌐
GitHub
github.com › facebook › react-native › issues › 10290
Dynamic Image Sources of Local Image Files · Issue #10290 · facebook/react-native
October 7, 2016 - Unable to display images from dynamic sources. I have tried using require, which isn't meant to work as I found in searching for how to resolve this. What I would like is to have the image files contained locally, but have the local paths stored on a external database accessed via a web service. We have an array containing the relative paths to the images: var BoardView = React.createClass({ getInitialState() { var tiles = [{src: './../images/treasurechest.png', title: 'Small Chest'}, {src: './../images/treasurechest.png', title: 'Big Chest'}]; return { clickCount: this.props.initialClickCount, tiles: tiles }; },
Author   zerosnap
🌐
Medium
careerwithvasanth.medium.com › how-to-use-local-images-dynamically-in-react-native-71b9f3b0db20
How to use local Images dynamically in React Native | by Vasanth Bhat | Medium
May 16, 2020 - “The only allowed way to refer to an image in the bundle is to literally write require(‘image!name-of-the-asset’) in the source.” · In case your thinking, what is this error message is, It means in react all the static Image path has to be specified during the compilation itself, anything specified dynamically is considered as an error.
🌐
Reddit
reddit.com › r/reactnative › dynamic path using require in rn image component
Dynamic path using require in RN Image component : r/reactnative
February 29, 2024 - When I fetch these details I apply the virtualPath dynamically to the uri/require of the source Image component. ... About image, do you use a picker to choose image from library or user select from a list you defined? File path should be absolute path to use as uri. Post your example data ... Looks like dynamic require paths are a no-go in React Native.
🌐
Reddit
reddit.com › r/reactnative › react native: dynamic image require in component?
r/reactnative on Reddit: React Native: dynamic image require in component?
December 2, 2020 -

Hi everybody, I'm trying to show a randomly generated image in my Image component, however I'm getting the following error:

invalid call at line 58:require(getRandomImage())

My component:

const [ randomImages, setRandomImages ] = useState([
        //require('../assets/images/aranda.jpg'),
        require('@assets/images/bilmore.jpg'),
        require('@assets/images/cachalote.jpg'),
        require('@assets/images/lola.jpg'),
        require('@assets/images/meson.jpg'),
        require('@assets/images/paloma.jpg'),
        require('@assets/images/picasso.jpg'),
        require('@assets/images/pimpi.jpg'),
        require('@assets/images/tintero.jpg'),
        require('@assets/images/tropicana.jpg'),
    ]);

const getRandomImage = () =>
    {
        return randomImages[rand(0,9)];
    };

    const rand = (min, max) => 
    {
        return Math.floor(Math.random() * (max - min)) + min;
    }


<Image style={{ width:'100%', height:200,}}  resizeMode="cover" source={require(getRandomImage())}/> 
🌐
Repeato
repeato.app › home › handling dynamic image imports in react native
Handling Dynamic Image Imports in React Native - Repeato
December 17, 2024 - class App extends Component { state = { avatar: "spiderman" } get avatarImage() { switch (this.state.avatar) { case "spiderman": return require('./spiderman.png'); case "batman": return require('./batman.png'); default: return require('./no-image.png'); } } render() { return <Image source={this.avatarImage} /> } } Whether you are optimizing image management or any other aspect of your React Native app, automated testing can significantly enhance your development process.