How to load local dynamic images in html in React Native

First we will need to use react-native-render-html

(I like it because it doesn’t use WebView, and WebView for this kinda thing is bad.)

But there is a problem, this lib doesn’t have a way to load local images out of the box. For that you need to create a custom image tag and add it to your HTML component

Like this:

Here i’m creating a custom tag named img inside renderers prop and defining it as an <Image> component with an image path that i have selected.

And that’s the result:

But the main problem isn’t solved, our goal is to render local images dynamically using that custom tag that we’ve created.

After this maybe you’re thinking:

-“Just pass the image path as a prop to the <img/> tag and voila, you’re done!”

OK, that sounds right, but how do I pass the image path that I want our custom tag to render?

I added a source prop with the image path in <img> tag.

And i am getting it inside our img function adding attribs as a parameter

Now i can access the image path using attribs.source

This will output in the console:

Now, if we try to change line 18 like this:

We are going to get this error:

That’s because all the imports have to be a string, not a dynamic expression.

Maybe now you’re thinking:

“-If he is writing a post on Medium its because there’s is a way to import dynamic imports inside require, right?”

Well, no…

Let me show you what we are going to do:

One thing we know for sure, you can normally import images like this:

So, i’m creating an object with all my images and it’s requires as properties of that object.

Now i can finally pass the image that we want to render in the source property in our custom <img/> tag.

We also need to modify the Image component to something like this:

Now, if i change

Th will be the result:


The best way to implement this solution is to isolate your imageList variable in another file, and add each one of your images and it’s paths manually or you can create a script that does all the hard job for you (like i did).

You can learn how to create this script on this tutorial.

You can find the project source code here

I hope this has helped you ;)

Software Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store