I have a problem with generated dynamic path to import file in React. I used map() function for generated dynamic part of code, which is a sequence of the same few div elements with an audio element inside this div. This code looks like this:
{soundsName.map((sound, i) => {
return (
<div
className="drum-pad"
onClick={this.soundPlay}
title={"Sound " + sound}
key={i}
>
<audio className="clip" id={sound}>
<source src={`s${i}`} type="audio/mpeg"></source>
</audio>
{sound}
</div>
);
})}
My problem is that the generated path in 'src' for 'source' element is wrong. The path is only the name of imported file, like 's1', 's2', 's3' and so on.
Files are imported like this:
import s0 from "./assets/sounds/s1.mp3";
import s1 from "./assets/sounds/s2.mp3";
import s2 from "./assets/sounds/s3.mp3";
import s3 from "./assets/sounds/s4.mp3";
import s4 from "./assets/sounds/s5.mp3";
import s5 from "./assets/sounds/s6.mp3";
import s6 from "./assets/sounds/s7.mp3";
import s7 from "./assets/sounds/s8.mp3";
import s8 from "./assets/sounds/s9.mp3";
I really don't know, what I am doing wrong :( Maybe some of you know that?
Thanks for any help.
importstatement to retrieve the files? Instead you could output the path to thesrcattribute directly as follows:src={`./assets/sounds/s${i}.mp3`}