Hi everyone!
I’m making my drum machine, and I have a problem with the event handler. Let me show you my code first and then explain. This is my single pad component:
import React, { Component } from 'react';
class Pad extends Component {
constructor (props) {
super(props);
this.handleKeyDown = this.handleKeyDown.bind(this);
}
handleKeyDown(event) {
if (event.key === this.props.letter) {
console.log('something happened')
}
}
componentDidMount() {
window.addEventListener("keydown", this.handleKeyDown)
}
render (){
return (
<div className="drum-pad">
<h1>{this.props.letter}</h1>
</div>
)
}
}
export default Pad;
If I console.log event.key, it properly logs the letter (a string saved in the container component state as value for ‘letter’ in an object, in an array of objects, each with keys ‘name’ and ‘letter’ for now, I just got started).
Also the h1 is displaying the letters as expected, so this.props.letter also works.
So why isn’t ‘something happened’ being logged to my console?
I’m sorry if this is a silly question, or if it’s written in a confusing way! Im very new at React and kinda confused myself.
Thanks a lot in advance!!