0

I would like to know how I could use a string key to call an svg element imported from the /assets/ folder.

Here are my imports of svg images :

import image1 from "assets/images/image1.svg";
import image2 from "assets/images/image2.svg";

And here is my loop to display image1 and image2

{data.map((data: object, index: number) => (
  <img src={`image${index}`}/>
))}

But it doesn't work because I have this :

<img src={'image1'}/>

But i want this :

<img src={image1}/>

Thanks.

1 Answer 1

3
import image1 from "assets/images/image1.svg";
import image2 from "assets/images/image2.svg";

const images={
image1,
image2
}

{data.map((data: object, index: number) => (
  <img src={images[`image${index}`]}/>
))}
Sign up to request clarification or add additional context in comments.

Comments

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.