0

Is there any way to load an image this way?

var x = './img/a.jpg';
<Image source={require(x)}/>

I need to use this because I want to load the image dynamically.

1
  • Are you using any package manager such as Webpack ? (This is a yes if you are using create-react-app) Commented Dec 13, 2017 at 13:06

1 Answer 1

1

Simply you can create object and use it as follows

const images = {
  foo: require('./img1.png'),
  bar: require('./img2.png'),
}

<Image source={images.foo} />
Sign up to request clarification or add additional context in comments.

2 Comments

i want to use a loop for load all the image from folder dont wanna do code for every image
@MudassirZakaria You can iterate through object keys,for (var key in images) { <Image source={images[key]} />; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.