r/reactjs • • Aug 01 '18

Beginner's Thread / Easy Question (August 2018)

Hello! It's August! Time for a new Beginner's thread! (July and June here)

Got questions about React or anything else in its ecosystem? Stuck making progress on your app? Ask away! We’re a friendly bunch. No question is too simple. You are guaranteed a response here!

Want Help on Code?

  • Improve your chances by putting a minimal example on to either JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new). Describe what you want it to do, and things you've tried. Don't just post big blocks of code.
  • Pay it forward! Answer questions even if there is already an answer - multiple perspectives can be very helpful to beginners. Also there's no quicker way to learn than being wrong on the Internet.

New to React?

Here are great, free resources!

27 Upvotes

568 comments sorted by

View all comments

Show parent comment

2

u/dceddia Aug 20 '18

You might want to try putting the eventListener on the 'body' element, like document.body.addEventListener('keypress', function() { console.log('yay') });. That'll catch keypresses anywhere in the document, since events in JS "bubble up" the DOM (unless an element in the path calls stopPropagation on the event, which you're not gonna be doing, so you should be good!)

1

u/Bylee_ Aug 20 '18

That exactly what I ended up doing !

Forgot to come back and update my post to let others, who might have the same issue, know how to fix it.

Thanks for your help.