简体   繁体   English

无法在 Drum Machine 项目中触发元素(React)

[英]Cannot trigger element in Drum Machine project (React)

My app is working fine, but an error is popping up and I am unable to solve it.我的应用程序运行良好,但弹出错误,我无法解决。 This is for a project and I have to pass this error.这是一个项目,我必须通过这个错误。 The error is as follows:错误如下:

Error: "When I press the trigger key associated with each.drum-pad, the audio clip contained in its child element should be triggered (eg pressing the Q key should trigger the drum pad which contains the string "Q", pressing the W key should trigger the drum pad which contains the string "W", etc.)."错误:“当我按下与 each.drum-pad 关联的触发键时,应触发包含在其子元素中的音频剪辑(例如,按下 Q 键应触发包含字符串“Q”的鼓垫,按下 W键应触发包含字符串“W”等的鼓垫)。

Code:代码:

import React from "react";
import ReactDom from "react-dom";

const sounds = [
  {
    idnum: "1",
    id: "Q",
    src: "https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3",
  },
  {
    idnum: "2",
    id: "W",
    src: "https://s3.amazonaws.com/freecodecamp/drums/Heater-2.mp3",
  },
  {
    idnum: "3",
    id: "E",
    src: "https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3",
  },
  {
    idnum: "4",
    id: "A",
    src: "https://s3.amazonaws.com/freecodecamp/drums/Heater-4_1.mp3",
  },
  {
    idnum: "5",
    id: "S",
    src: "https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3",
  },
  {
    idnum: "6",
    id: "D",
    src: "https://s3.amazonaws.com/freecodecamp/drums/Dsc_Oh.mp3",
  },
  {
    idnum: "7",
    id: "Z",
    src: "https://s3.amazonaws.com/freecodecamp/drums/Kick_n_Hat.mp3",
  },
  {
    idnum: "8",
    id: "X",
    src: "https://s3.amazonaws.com/freecodecamp/drums/RP4_KICK_1.mp3",
  },
  {
    idnum: "9",
    id: "C",
    src: "https://s3.amazonaws.com/freecodecamp/drums/Cev_H2.mp3",
  },
];

class SoundButton extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      audioSource: "not clicked",
    };
    this.soundOn = this.soundOn.bind(this);
  }

  buttonRef = React.createRef();
  audioRef = React.createRef();

  buttonRef(e) {
    e.click();
  }

  soundOn() {
    console.log(this.audioRef.current);
    this.audioRef.current.play();
  }

  render() {
    const { info } = this.props;

    return (
      <button
        ref={this.buttonRef}
        className="drum-pad"
        id={info["idnum"]}
        onClick={this.soundOn}
      >
        {info["id"]}
        <audio
          ref={this.audioRef}
          src={info.src}
          className="clip"
          id={info.id}
          type="audio/mp3"
        ></audio>
      </button>
    );
  }
}
class Button extends React.Component {
  // any other logic

  render() {
    return sounds.map((info) => <SoundButton info={info} key={info.id} />);
  }
}
export default Button;

My app is working fine, but I need your help in solving the error.我的应用程序运行良好,但我需要您的帮助来解决错误。 Please help me with it.请帮帮我。

Thanks!谢谢!

Please look at that code .请看那个代码

If you will to handle keyboards event you should listen for it window.addEventListener("keyup", this.onKeyUp);如果你要处理键盘事件,你应该监听它window.addEventListener("keyup", this.onKeyUp); and handle event as you wish, for example get key code from event and use them to play drum.并根据需要处理事件,例如从事件中获取键码并使用它们来打鼓。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM