I am working on my Drum Machine react app, and I am trying to get the state change from a click event in my parent/container component down to one of the AudioButton/drum button components that I mapped out with an external array, but I’ve had a hard time figuring this out. I don’t even know if mapping out the drum buttons are the best way to go about this project. I’d appreciate any help with trying to at least pass down the state info from the parent down to, at the very least, all of the children or, ideally, the particular child which was clicked on, although I imagine using a conditional in conjunction with array.some() would be the solution for sending the event data down to the correct button.
Current code @ codesandbox: https://codesandbox.io/s/8ylljx38p9, or
Container.js:
import React from "react";
import AudioButton from "./AudioButton";
import SoundData from "./SoundData";
class Container extends React.Component {
constructor(props) {
super(props);
this.state = {
play: false,
SoundData
};
this.playSound= this.playSound.bind(this)
}
playSound(e){
this.setState({play: true});
audio.play();
console.log(e);
}
render() {
const ButtonComponents = SoundData.map(button => (
<AudioButton
key={button.id}
id={button.id}
clipSrc={button.clipSrc}
clipName={button.clipName}
onClick={this.playSound}
/>
));
return (
<div id="drum-machine">
<main id="display">{ButtonComponents}</main>
</div>
);
}
}
export default Container;
AudioButton.js:
import React from "react";
class AudioButton extends React.Component {
constructor(props) {
super(props);
//this.togglePlay = this.togglePlay.bind(this);
}
audio = new Audio(this.props.clipSrc);
/* togglePlay = () => {
this.setState({ play: true },
() => {this.state.play === true && this.audio.play(); //short circuited ternary
//console.log(this.props.clipName);
});
}; */
render() {
return (
<div>
<button
onClick={this.props.playSound}
className="clip myButton"
id={this.props.id}
>
{this.props.clipName}
</button>
</div>
);
}
}
export default AudioButton;