Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

HTML sound element not getting its source from a url passed from react

I am building this drumpad in react in codepen. But when I pass a prop, to the audio element’s source child, it doesn’t seem it gets any link to an audio file. I tried putting in the url where the props.audioSource is in , and then it worked.

Is some quirk in react, that does so you can’t pass url’s to html’s audio element?

The project on codepen

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const DrumTileComponent = (props) => {
  return (
    <div className=".drum-pad" id="{props.drumPadId}">
      <div> 
        <audio className="clip" id="{props.drumPadId}" controls> 
          <source src="{props.audioSource}"  type="audio/mp3" />
          Your browser does not support the audio element.
        </audio>
      </div>
    </div>
  );
};

const DisplayComponent = (props) => {
  return (
    <div id="display">
      
    </div>
  );
};

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      
    }
  }
  
  render() {
    return (
      <div id="drum-machine">
        <DrumTileComponent drumPadId="A" audioSource="https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3" />
        Hello
      </div>
    );
  };
};
ReactDOM.render(<App />, document.getElementById("App"));

>Solution :

You can pass source as a variable without wrapping it with string

const DrumTileComponent = (props) => {
  return (
    <div className=".drum-pad" id="{props.drumPadId}">
      <div> 
        <audio className="clip" id="{props.drumPadId}" controls> 
          <source src={props.audioSource}  type="audio/mp3" />
          Your browser does not support the audio element.
        </audio>
      </div>
    </div>
  );
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading