1.2 - Clock App
This next time app we're going to make is going to tell us what time it is! So a little bit more complex than a simple timer, but still utilizing many of the same lifestyle methods.
Component Starter
To start we need to create a ClockApp.js
file inside of our timer-apps folder. Your timer-apps should look like the following.
Let's go ahead and get our ClockApp
file started with the following code:
Next, to get this to display, we need to change our parent component, TimePiecesApp to no longer have CloakApp commented out, so go ahead and make sure that your TimePiecesApp file looks like this.
Now you should see your Clock App h1, to know you've correctly gotten your component to display!
State Set up
For this app, our goal is to display the current time. To do this, we need to set up our state in this component to be the current time, and then have that time be displayed in our render.
So, the first thing we can do is set up our state to have our current time. As usual, to initiate state, we can do this inside of our constructor. Remember to include super(props)
inside of your constructor as well. Type the following code at the top of your ClockApp class.
So now our state should reflect the current time. But we haven't rendered this to the screen yet, so let's go ahead and add it to our render. See if you can figured out how to add it without looking at the below code. Remember to use this, when needed.
Did you add it successfully? Either way, check out the below code for our render() function, to compare to yours.
Time Check
Now that we have it rendering the time, do you think this will update to the new time? If you're unsure, watch and see what happens, are the seconds moving?
Why isn't it updating with the time? Think back to our timer app, and remember what we had to do to get the seconds to update every single second. We'll need to do something similar here, to get our clock to update every single second. Just like the simple timer app, we'll need to use componentDidMount()
to set our interval, and componentWillUnmount()
to stop the interval when we navigate away or the component is no longer needed.
componentDidMount
In our componentDidMount
we need some way to update our time part of our state every second with the current updated time. In the below code, we are ensuring that this happens every one second.
Notice the first line of ComponentDidMount()
, why are we creating a new const _this? Try commenting out the const _this = this
and using this
instead of _this
in the rest of this block of code. It throws an error, and says that this.getTimeString() is not a function
.
Why does it do this? This is due to scoping in JavaScript. Where we set _this = this, this
is in the global scope, but inside of the function(), we now have a local scope, so if we try to use regular this
inside of the function, it is pointing to the local scope of the function, which is not what we want.
Check the clock
Now that we have our setInterval with our date, we are correctly able to see a clock that updates!! Now, just like our simple timer app, we need to make sure that when we leave the page, whent the component unmounts, that the setInterval is not still going. So again, we're going to clearInterval in componentWillUnmount.
And that takes care of our clock app! Hopefully you have a better understanding of some of the lifestyle methods in react, and some more information on this scoping JS. Check your final code against the code below, to ensure that it's all correct. Next up is a stop watch app!
Last updated