Map a Sequence of Values in React with useBroadcaster

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet

To display a sequence of values in React, we can use our mapSequence operator, wrap it around a broadcaster, then pass that new broadcaster into our useBroadcaster we defined. This will demonstrate that any broadcaster we've defined before should work directly inside of React, but also show that we haven't yet handled the "done" scenario inside of our useBroadcaster.

John Lindquist: [0:00] Let's try bringing in our map sequence demo from before but this time use it inside of our useBroadcaster.

[0:08] We used map sequence with for...of. We did that on a message that was "Hi, my name is John." There we split that into an array. Then we also defined a function called delayMessage(), which took a value, hardcoded it to a createTimeout of 500 ms.

[0:38] Then our broadcaster became a map sequence where it took the word, passed that into delayMessage. That took the word, which since this argument and this argument line up perfectly we can just write this like this.

[0:58] Then we for...of-ed on the message. If I did everything right, I can take this broadcaster and just drop it in useBroadcaster. Paste it there. Hit Save.

[1:12] Looks like I forgot to import hardcode. We'll go ahead and do that, hardcode. Now we'll hit Save. Now, Hi, my name is John, or at least I thought my name was John. Turns out that in our broadcasters, for useBroadcaster, we're passing done directly into setState.

[1:33] We'd want to define this as value, so that if the value is done, we could do something. For right now, I'm just going to return and ignore it. If it's not done, then we can set the state to value. This means if I hit Save, Hi, my name is John, we've prevented this value of done being passed into setState.

[1:59] If we do run into a scenario where we need to handle done at the end of a broadcaster, we'll take care of that when we get there.