# React Drum Machine app: sending parent state down to a particular mapped component

**URL:** <https://forum.freecodecamp.org/t/react-drum-machine-app-sending-parent-state-down-to-a-particular-mapped-component/268480>\
**Category:** General\
**Created:** [March 31, 2019, 9:36pm UTC](https://forum.freecodecamp.org/t/react-drum-machine-app-sending-parent-state-down-to-a-particular-mapped-component/268480 "2019-03-31T21:36:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephepush](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/stephepush/32/90326_2.png) [@stephepush](https://forum.freecodecamp.org/u/stephepush)\
**Post date:** [March 31, 2019, 9:36pm UTC](https://forum.freecodecamp.org/t/react-drum-machine-app-sending-parent-state-down-to-a-particular-mapped-component/268480/1 "2019-03-31T21:36:49Z")

</div>

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](https://codesandbox.io/s/8ylljx38p9), or

Container.js:

```auto
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:

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![camelcamper](https://avatars.discourse-cdn.com/v4/letter/c/f0a364/32.png) [@camelcamper](https://forum.freecodecamp.org/u/camelcamper)\
**Post date:** [April 1, 2019, 7:46am UTC](https://forum.freecodecamp.org/t/react-drum-machine-app-sending-parent-state-down-to-a-particular-mapped-component/268480/2 "2019-04-01T07:46:40Z")

</div>

I think you need to pass the function as a prop, so change the `onclick` to a prop called `playSound`

```auto
const ButtonComponents = SoundData.map(button => (
      <AudioButton
        key={button.id}
        id={button.id}
        clipSrc={button.clipSrc}
        clipName={button.clipName}
        playSound={this.playSound} //makes this accessible in the child component as `this.props.playSound`
      />
    ));

```

Because in your `AudioButton` component you are already calling the function as a prop in a onclick event

```auto
render() {
    return (
      <div>
        <button
          onClick={this.props.playSound} //already calling functino as prop in onclick event
          className="clip myButton"
          id={this.props.id}
        >
          {this.props.clipName}
          
        </button>
      </div>
    );
  }

```

---

<div class="post-metadata">

**Author:** ![camelcamper](https://avatars.discourse-cdn.com/v4/letter/c/f0a364/32.png) [@camelcamper](https://forum.freecodecamp.org/u/camelcamper)\
**Post date:** [April 1, 2019, 7:56am UTC](https://forum.freecodecamp.org/t/react-drum-machine-app-sending-parent-state-down-to-a-particular-mapped-component/268480/3 "2019-04-01T07:56:02Z")

</div>

I just realized i may have failed to comprehend your question.

What exactly are you trying to pass from the parent component to the child components?

---

<div class="post-metadata">

**Author:** ![anon96529406](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@anon96529406](https://forum.freecodecamp.org/u/anon96529406)\
**Post date:** [April 1, 2019, 9:34am UTC](https://forum.freecodecamp.org/t/react-drum-machine-app-sending-parent-state-down-to-a-particular-mapped-component/268480/4 "2019-04-01T09:34:56Z")

</div>

Hello, I’m afraid I don’t quite understand your problem; can you be more concrete with what is it that you’re trying to achieve (e.g. getting a sound to play but failing, or getting a unique string to display but to no avail)? And what tests are failing?

But I took a wild guess anyway; assuming what you’re trying to do is to pass `this.state.play` as part of the logic to display a unique string when each sound is played (cuz the current code in the sandbox doesn’t do that), you can pass the state down to the child components by doing this:

```auto
      <AudioButton
        key={button.id}
        id={button.id}
        isAudioPlaying={this.state.play}
        clipSrc={button.clipSrc}
        clipName={button.clipName}
        onClick={this.playSound}
      />

```

this way, each AudioButton child component will keep track of `this.state.play`, and you can use this state to perform the display logic.

Right now, from what I’ve seen, you’ve set up the state, have a method to change it, but the state is not used any where in your app.

Hope this can help. Cheers!

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 8:03pm UTC](https://forum.freecodecamp.org/t/react-drum-machine-app-sending-parent-state-down-to-a-particular-mapped-component/268480/5 "2021-01-17T20:03:33Z")

</div>

Closed on January 17, 2021, 8:03pm UTC


