Create a Broadcaster in React with useState and useEffect Hooks

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 4 years ago

Combing the useState and useEffect hooks allows use to manage asynchronous state. This lesson walks through using a broadcaster inside of useEffect then using setState as the listener.

Instructor: [0:00] To add a broadcaster to React, let's add some state first. Go ahead and return this JSX, bring in the useState hook, and then just set up the basic useState. We can start it with high and say that state is high. setState would allow us to change that. Then we'll change this to state, hit Save. Now we have high.

[0:28] Let's use a createInterval broadcaster, which I'll import. We want our createInterval to call setState at the end. Essentially, something that looks like createInterval. We'll do a time of one second and then just imagine that this calls setState. We can do this behavior inside of a useEffect hook, which I can put after my useState hook.

[0:59] Go ahead and type useEffect, and this takes a function. Inside of that function, we'll go ahead and paste our createInterval setState.

[1:10] Now if I hit Save, see high, then , then 1, at least trying to change to 1. The reason this behavior goes into full panic mode is because the second parameter of useEffect accepts an array of dependencies that tells it when it should update.

[1:32] If I hit Save now, you'll see it works fine because there is nothing in this array telling it to re-render once the state changes. Before, this was trying to set this up every single time the timer updated.

egghead
egghead
~ an hour 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