Skip to content
Advertisement

How can I show dynamic images with require() in React Native?

I’m fetching data from api and then i want to show images based on the symbol coming from the api data

JavaScript

sending the path and the symbol to another component VVVV

JavaScript

Is there a way to do it like that or i have to use something else ?

Advertisement

Answer

“In order for this to work, the image name in require has to be known statically”, that’s what React Native’s doc says. In others words, you cannot require an image in React Native with a dynamic path.

The workaround in your case is to import them statically in an object, where each key is a symbol and its value the path of its related image. Don’t forget to use the correct path:

JavaScript

This way you give Listing only the symbol, and it will fetch the image from the above object:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement