From the course: React Hooks

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Solution: Working with timers

Solution: Working with timers - React.js Tutorial

From the course: React Hooks

Solution: Working with timers

(upbeat music) - [Instructor] UseEffect hooks are really helpful when we're dealing with asynchronous data. Now, that could be something like a fetch for an external API or that could be working with a timer. So we're going to go ahead and work with a timer today. And we're going to borrow from this function that was already here, the loadTimerData. And this is going to set a time out and that'll happen after two seconds. Now, within our function, this App function, we need to create a bunch of different useState variables. So those are going to look like this. We'll create one called seconds and that'll be a function to update the state called setSeconds. And we'll set that equal to useState with an initial state of zero. We'll also use loading and setLoading. useState, this time, will take in true and then we'll say timerData and setTimerData also should be set to useState and the initial state should be null. So from here, I can actually use this within our little return here and…

Contents