React - How to play sounds?

Does anyone know how to play Audio files from React?
The HTML5 audio tag doesn’t seem to work in JSX and I can’t find another solution. I’m trying to avoid interacting with the DOM directly if possible.


1 Like

Also tried:
const sounds = {
green: new Audio(“”),
blue: new Audio(‘’),
pink: new Audio(‘’),
yellow: new Audio(‘’)
this.handleClick(color) => sounds[color].play

Which plays the sound, but totally destroys the rest of my game logic (side effects)

I came up with a solution using refs in the end. It doesn’t feel quite right, but it’s better than nothing.