Tell us what’s happening:
I’m trying to tackle the Drum Machine challenge with React. It was pretty easy to deal with the click on pads, but much tougher to handle the keyPress.
I did something that “works”, but I ended up with this error message in the console:
Warning: Cannot update a component (App
) while rendering a different component (Pad
). To locate the bad setState() call inside Pad
, follow the stack trace as described in Bug: too hard to fix "Cannot update a component from inside the function body of a different component." · Issue #18178 · facebook/react · GitHub
While it doesn’t prevent the code from running, it still looks pretty ugly. I’ve identified that it’s this logic in my component that is creating the issue, but I don’t see any other options at the moment
if (keyPressed===Number(keyCode)) {
setKeyPressed("");
const audio = new Audio(sound);
audio.play();
setDisplay(id);
};
I understand is a bit long to review it and I hope my message is making sense. In any case, many thanks for checking this out and happy coding everyone!!
Your code so far
App.js
import './App.css';
import {useState, useEffect} from "react";
import sound1 from "./assets/sound1.mp3";
import sound2 from "./assets/sound2.mp3";
import sound3 from "./assets/sound3.mp3";
import sound4 from "./assets/sound4.mp3";
import sound5 from "./assets/sound5.mp3";
import sound6 from "./assets/sound6.mp3";
import sound7 from "./assets/sound7.mp3";
import Pad from './components/pad';
function App() {
const [display, setDisplay] = useState("Play your sound");
const [keyPressed, setKeyPressed] = useState("");
useEffect(() => {
const handleKeyDown = (e) => {
setKeyPressed(e.keyCode);
};
document.addEventListener("keydown", handleKeyDown);
return () => {
document.removeEventListener("keydown", handleKeyDown);
};
}, []);
return (
<div className="container">
<div id="drum-machine">
<div id="display">{display}</div>
<div id="drum-pads">
<Pad setDisplay={setDisplay} keyPressed={keyPressed} setKeyPressed={setKeyPressed} id="Work it" keyCode="65" sound={sound1} />
<Pad setDisplay={setDisplay} keyPressed={keyPressed} setKeyPressed={setKeyPressed} id="Make it" keyCode="90" sound={sound2} />
<Pad setDisplay={setDisplay} keyPressed={keyPressed} setKeyPressed={setKeyPressed} id="Do it" keyCode="69" sound={sound3} />
<Pad setDisplay={setDisplay} keyPressed={keyPressed} setKeyPressed={setKeyPressed} id="Makes us" keyCode="82" sound={sound4} />
<Pad setDisplay={setDisplay} keyPressed={keyPressed} setKeyPressed={setKeyPressed} id="Harder" keyCode="81" sound={sound5} />
<Pad setDisplay={setDisplay} keyPressed={keyPressed} setKeyPressed={setKeyPressed} id="Better" keyCode="83" sound={sound6} />
<Pad setDisplay={setDisplay} keyPressed={keyPressed} setKeyPressed={setKeyPressed} id="Faster" keyCode="68" sound={sound7} />
<Pad setDisplay={setDisplay} keyPressed={keyPressed} setKeyPressed={setKeyPressed} id="Stronger" keyCode="70" sound={sound7} />
<Pad setDisplay={setDisplay} keyPressed={keyPressed} setKeyPressed={setKeyPressed} id="More than" keyCode="87" sound={sound7} />
<Pad setDisplay={setDisplay} keyPressed={keyPressed} setKeyPressed={setKeyPressed} id="Faster" keyCode="88" sound={sound7} />
<Pad setDisplay={setDisplay} keyPressed={keyPressed} setKeyPressed={setKeyPressed} id="Stronger" keyCode="67" sound={sound7} />
<Pad setDisplay={setDisplay} keyPressed={keyPressed} setKeyPressed={setKeyPressed} id="More than" keyCode="86" sound={sound7} />
</div>
</div>
</div>
);
}
export default App;
pad.js
const Pad = ({setDisplay, keyPressed, setKeyPressed, id, keyCode, sound}) => {
const handleClick = (e) => {
e.target.children[0].play();
setDisplay(id);
};
if (keyPressed===Number(keyCode)) {
setKeyPressed("");
const audio = new Audio(sound);
audio.play();
setDisplay(id);
};
return (
<button onClick={handleClick} id={id} className="drum-pad">
{id}
<audio src={sound}></audio>
</button>
)
};
export default Pad;
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36
Challenge: Build a Drum Machine
Link to the challenge: