React Calculator onKeyDown & feedback

Hi, I would like a feedback about my jS Calculator and ask a question about onKeyDown. I implemented onKeyDown in DrumMachine, but actually now I’m doing it in an opposit way.

In the Drum Machine the onKeyDown handler is direct in the child, and call the methods that plays the audio.

In the Calculator I was trying to do it in the opposite way. All the methods are in the parent, and they are passed to the buttons. But actually, I’m not sure about what I’m doing with onKeyDown.
What I’m doing, maybe is, that I grasp the key values from the Children, and I send them to the Parent’s method, which is the handlerOnKeyDown. So when I click a number on my keyboard, the handler is called with the correct value which is contained in e.key.
What I have to do than, is call inside the handlerOnKeyDown the other methods which handle numbers, operators, decimal, clear and equals. But actually every of this method, is based on the, so when I call for example this.handleNumber(e.key), it’s not automatically read as I would like, as a Number.

So I was thinking, to make an if statement in each method, which tells:

  • if the argument is already a number, then use it!
  • else (the argument is the onClick event), extract the textContent and use that.

But actually, it seems to me too messy.

Suggestion ?


This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.