I'm trying to learn React and im working on a FreeCodeCamp.com project to build a drum machine. im trying to follow a demo video, what I have and the demo has looks the same except that the demo version works and mine does not. the code should render a set of buttons that are linked to an array of object's which contain links to audio clips. the code renders the buttons correctly but the sound wont play. I will also link the [Codepen][1] im building it on incase that helps. Any help would be greatly appreciated. this is what I have so far.
const bankOne = [
{
keyCode: 81,
keyTrigger: 'Q',
id: 'Heater-1',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3'
},
{
keyCode: 87,
keyTrigger: 'W',
id: 'Heater-2',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-2.mp3'
},
{
keyCode: 69,
keyTrigger: 'E',
id: 'Heater-3',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3'
},
{
keyCode: 65,
keyTrigger: 'A',
id: 'Heater-4',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-4_1.mp3'
},
{
keyCode: 83,
keyTrigger: 'S',
id: 'Clap',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3'
},
{
keyCode: 68,
keyTrigger: 'D',
id: 'Open-HH',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Dsc_Oh.mp3'
},
{
keyCode: 90,
keyTrigger: 'Z',
id: "Kick-n'-Hat",
url: 'https://s3.amazonaws.com/freecodecamp/drums/Kick_n_Hat.mp3'
},
{
keyCode: 88,
keyTrigger: 'X',
id: 'Kick',
url: 'https://s3.amazonaws.com/freecodecamp/drums/RP4_KICK_1.mp3'
},
{
keyCode: 67,
keyTrigger: 'C',
id: 'Closed-HH',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Cev_H2.mp3'
}
];
const bankTwo = [
{
keyCode: 81,
keyTrigger: 'Q',
id: 'Chord-1',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Chord_1.mp3'
},
{
keyCode: 87,
keyTrigger: 'W',
id: 'Chord-2',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Chord_2.mp3'
},
{
keyCode: 69,
keyTrigger: 'E',
id: 'Chord-3',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Chord_3.mp3'
},
{
keyCode: 65,
keyTrigger: 'A',
id: 'Shaker',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Give_us_a_light.mp3'
},
{
keyCode: 83,
keyTrigger: 'S',
id: 'Open-HH',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Dry_Ohh.mp3'
},
{
keyCode: 68,
keyTrigger: 'D',
id: 'Closed-HH',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Bld_H1.mp3'
},
{
keyCode: 90,
keyTrigger: 'Z',
id: 'Punchy-Kick',
url: 'https://s3.amazonaws.com/freecodecamp/drums/punchy_kick_1.mp3'
},
{
keyCode: 88,
keyTrigger: 'X',
id: 'Side-Stick',
url: 'https://s3.amazonaws.com/freecodecamp/drums/side_stick_1.mp3'
},
{
keyCode: 67,
keyTrigger: 'C',
id: 'Snare',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Brk_Snr.mp3'
}
];
const KeyboardKey = ({play, sound: {keyTrigger, url} }) => {
return (
<button className="drum-pad" onClick={()=> play(keyTrigger)} >
<audio className="clip" id={keyTrigger} src={url} />
{keyTrigger}
</button>
)
}
const Keyboard = ({play}) =>{
return bankOne.map((sound) => <KeyboardKey play={play} sound={sound} />)
}
const App = () => {
const play = (keyTrigger) => {
const audio = document.getElementById(keyTrigger)
audio.currentTime = 0;
audio.play()
}
return <div id="drum-machine">
<Keyboard />
</div>
}
ReactDOM.render(<App />, document.getElementById("drum-machine")); ```
[1]: https://codepen.io/AlakT/pen/QWaRRwe
CodePudding user response:
You forgot to pass play function as prop
const App = () => {
const play = (keyTrigger) => {
const audio = document.getElementById(keyTrigger)
audio.currentTime = 0;
audio.play()
}
return <div id="drum-machine">
<Keyboard play={play} /> // passing play function to <Keyboard />
</div>
}