Explaining the useListener Code

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

We went through creating custom React hooks to support our callback and closure patterns pretty quickly. Let's take a second to review how everything fits together before we move on.

Instructor: [0:00] I want to be clear on how useListener and useBroadcaster work together. If we look at useListener and its internal callback listener, this might look more familiar if this was renamed to broadcaster and then this was renamed to listener.

[0:19] When used in this way, this callback can act as a broadcaster that receives a listener of setState. The first time this function is called is this exact line where setState is passed into or up to this function. This setState, which originates from here, arrives here and is assigned to our listener.

[0:47] In the future, we'll undo those, this block is ignored and the listener itself is simply a call to setState. This might make more sense if you looked at this as setState because on that first pass, when this is invoked with setState, then it's assigned, but in subsequent passes, the values come through, and we just pass those values to setState.

[1:16] The goal of useListener was to write a callback that could be fired by these events, then also use it as a broadcaster, and allow us to use our operators on that callback. The solution here, which is a bit clever, which I try to avoid...

[1:35] Since on this first pass, this function is a broadcaster, and on the second pass, it's a listener. On the first pass, this is a listener. On the second pass, that value is going into the listener defined. That's the solution I came up with so that useListener and useBroadcaster could work together inside of the React model.

egghead
egghead
~ 11 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