Handling an Enter Keypress with useListener and React

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 4 years ago

You often want to ignore values until the user performs a certain action. This lesson walks through setting up an allowWhen broadcaster that will only pass the latest value through when another broadcaster fires. The scenario is using an Enter keypress to allow the latest value of the input text to be passed through.

Instructor: [0:00] Since we have a text box, let's put it to good use by allowing ourselves to type something into the text box. Hit Enter and show all those words in sequence. My first thought is that, we need to wait to detect. When we hit enter, we're going to do this onkeypress and set up an onkeypress listener up here.

[0:20] We'll say, "Let onkeypress, and this is a useListener." If the idea is to get the text off of onInput, that means we can bring in target values. We'll do that here, target value. I'll call this input value and wrap target value around onInput.

[0:43] Onkeypress, I only want that to broadcast when the enter key comes through. We'll bring in filter and save that enter is filtered by an event where the event.key is equal to enter, and then wrap that around onkeypress.

[1:06] The way these two work together can look something like an allow when operator, where I can have an allow broadcaster, then the main broadcaster listener, then we can track the current value from our broadcaster where we just say that current is equal to the value.

[1:28] Our allow broadcaster would just ignore any values, but instead send the current down to the listener. You think of this as the input event getting the text from the text field, and then this as the Enter key, which will take the current text and then just pass that along.

[1:49] We can use this together inside of here where I can take the input value, and then wrap that with an allow when you hit Enter. Hit Save there, and now type something. Hit the Enter key, and then the text shows below. Type another thing. Hit Enter, and the net text changes.

egghead
egghead
~ 43 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today