How to dynamically render/ import images in react / typescript?
I'm using Typescript with React.
Following is what I want to achieve.
but this is not working. It is not resolving the expression { 'assets/icons/' + media + '.svg'} .
<div className="medias">
MEDIAS.map((media) =>
(<img src={ 'assets/icons/' + media + '.svg'} />)
)
</div>
This is what I've tried.
const Medias = async () => {
return (
<div className="medias">
{await Promise.all(
MEDIAS.map((media) =>
import(`assets/icons/${media}.svg`).then((v) => v.default)()
).map((item) => <img src={media} />)
)}
</div>
);
};
export default Medias
I want to dynamically import and render images based on the above logic.
In angular or vue this can be achieved easily inside templates. But here in react it seems like not working.
But seems like it is not working.
Is there a work around?